2011-11-10 125 views
34

可能重複一個心臟:
Equation-driven smoothly shaded concentric shapes情節中的R

我怎麼可能R中繪製對稱的心臟像我繪製一個圓(使用plotrix)或矩形?

我想爲此編寫代碼,以便我可以爲自己做到這一點,並能夠將其推廣到類似的未來需求。我見過比這更精緻的情節,所以它非常可行,只是我缺乏這方面的知識。

+29

Valetines天仍3個月關,丘比特一定打你漂亮哈RD。 – Johan

+6

不確定在'r'中的實現,但是你可能會對[cardiod polar equation](http://mathworld.wolfram.com/Cardioid.html)和/或[其他路徑]感興趣(http:///mathworld.wolfram.com/HeartCurve.html)。 –

+0

我使用上述鏈接中Wolfram頁面中的最後一個eqn作爲參數化eqn的來源,並且僅在0 - > 2pi的範圍內進行計算。 –

回答

72

這是繪製「參數方程」的示例,即共享共同參數的x和y的兩個單獨方程的配對。您可以找到許多可以在此框架內編寫的常見曲線和形狀。

dat<- data.frame(t=seq(0, 2*pi, by=0.1)) 
xhrt <- function(t) 16*sin(t)^3 
yhrt <- function(t) 13*cos(t)-5*cos(2*t)-2*cos(3*t)-cos(4*t) 
dat$y=yhrt(dat$t) 
dat$x=xhrt(dat$t) 
with(dat, plot(x,y, type="l")) 

Other Parametric (and implicit and polar) Heart Eqns

你也可以使用「填充」的polygon功能的能力「熱起來」:

with(dat, polygon(x,y, col="hotpink")) 

如果你只是想幼小的心靈在灑左右不同的地方,你可以在看help(points)頁面和使用TestChars功能後使用Symbol字體版本的「heart」:

points(c(10,-10, -15, 15), c(-10, -10, 10, 10), pch=169, font=5) 

enter image description here

Windows用戶可能想看看是否添加開羅包幫助卡符號增益獲得包括「心」。(當我測試了TestChars功能對我的MacPro的WinXP的「側面」我做沒有得到心,通過MS-Word中的「特殊符號」分頁並沒有發現任何東西。所以我搜索了Rhelp並找到了Ivo Welch最近發佈的帖子。他報告了一個錯誤,但他們在我的機器上看起來沒問題。)進一步說明......我認爲他的心中和鑽石編碼是相反的。

library(Cairo) 

clubs <- expression(symbol('\247')) 
hearts <- expression(symbol('\251')) 
diamonds <- expression(symbol('\250')) 
spades <- expression(symbol('\252')) 
csymbols <- c(clubs, hearts, diamonds, spades) 

plot(0, xlim=c(0,5), ylim=c(0,2), type="n") 
clr <- c("black", "red", "red", "black") 
for (i in 1:4) { 
    hline <- function(yloc, ...) 
     for (i in 1:length(yloc)) 
      lines(c(-1,6), c(yloc[i],yloc[i]), col="gray") 
       hline(0.9); 
       hline(1.0); 
       hline(1.1); 
       hline(1.2) 
text(i, 1, csymbols[i], col=clr[i], cex=5) 
text(i, 0.5, csymbols[i], col=clr[i]) } 

# Also try this 
plot(1,1) 
text(x=1+0.2*cos(seq(0, 2*pi, by=.5)), 
    y=1+0.2*sin(seq(0, 2*pi, by=.5)), 
        expression(symbol('\251'))) 

enter image description here

+0

我其實想要小符號,但我機器上的169是版權符號「©」。用'points(x,y,pch)'做一個心臟符號需要做些什麼?我認爲字體家族或類似的東西,但我不確定如何解決這個問題。 –

+0

@ aL3xa的回答中的評論(#2)中的鏈接可能很有用... –

+0

@Tyler Rinker:在幫助(點)的示例末尾定義並運行函數「TestChars(font = 5)頁。它會打印出您的系統的所有符號索引的十進制數字:'c(32:126,160:254)' –

16

簡單而醜陋的黑客:

plot(1, 1, pch = "♥", cex = 20, xlab = "", ylab = "", col = "firebrick3") 
+1

免責聲明:我非常清楚的事實,這是不是你想要什麼,我真的很想看到你正在尋找的實際代碼。 =) – aL3xa

+2

但是這實際上可能是一個非常好的起點,如果一個人想用心作爲陰謀角色:請參閱http://stackoverflow.com/questions/5886018/using-unicode-dingbat-like-glyphs-in-r-圖形跨設備的平臺-E和http://www.fileformat.info/info/unicode/char/search.htm?q=heart&preview=entity –

+5

但如果走路像一個心臟,叫聲也像心臟,它必須是一顆心。 :) –

11

這裏是一個ggplot心:

library(ggplot2) 

dat <- data.frame(x=seq(0, 2*pi, length.out=100)) 
cardioid <- function(x, a=1)a*(1-cos(x)) 
ggplot(dat, aes(x=x)) + stat_function(fun=cardioid) + coord_polar() 

enter image description here

和心臟圖(由@BenBolker鏈接):

heart <- function(x)2-2*sin(x) + sin(x)*(sqrt(abs(cos(x))))/(sin(x)+1.4) 
ggplot(dat, aes(x=x)) + stat_function(fun=heart) + coord_polar(start=-pi/2) 

enter image description here

17

From a blog post:

求解參數方程爲Y(這樣做讓數學格式?)

的x^2 +(5Y/4-SQRT(ABS(X)))^ 2 = 1個

SQRT(1-X^2)= 5Y/4 - SQRT(ABS(X))

Y = 8/10 *(SQRT(1-X^2)+ SQRT(ABS(X)))

MASS::eqscplot(0:1,0:1,type="n",xlim=c(-1,1),ylim=c(-0.8,1.5)) 
curve(4/5*sqrt(1-x^2)+sqrt(abs(x)),from=-1,to=1,add=TRUE,col=2) 
curve(4/5*-sqrt(1-x^2)+sqrt(abs(x)),from=-1,to=1,add=TRUE,col=2) 

enter image description here

4

如果要更 「成熟」,嘗試以下內容(幾年前發佈到R-help):

thong<-function(h = 9){ 
    # set up plot 
    xrange=c(-15,15) 
    yrange=c(0,16) 
    plot(0,xlim=xrange,ylim=yrange,type='n') 

    # draw outer envelope 
    yr=seq(yrange[1],yrange[2],len=50) 
    offsetFn=function(y){2*sin(0+y/3)} 
    offset=offsetFn(yr) 
    leftE = function(y){-10-offsetFn(y)} 
    rightE = function(y){10+offsetFn(y)} 

    xp=c(leftE(yr),rev(rightE(yr))) 
    yp=c(yr,rev(yr)) 
    polygon(xp,yp,col="#ffeecc",border=NA) 

    # feasible region upper limit: 
    # left and right defined by triple-log function: 
    xt=seq(0,rightE(h),len=100) 
    yt=log(1+log(1+log(xt+1))) 
    yt=yt-min(yt) 
    yt=h*yt/max(yt) 
    x=c(leftE(h),rightE(h),rev(xt),-xt) 
    y=c(h,h,rev(yt),yt) 
    polygon(x,y,col="red",border=NA) 
} 
+0

此代碼爲我生成錯誤。 – MYaseen208

+0

@ MYaseen208:我可能在剪切/粘貼中刪除了一個字符。原始代碼當然有效。你得到了什麼錯誤? –

7

另一種選擇,

xmin <- -5 
xmax <- 10 
n <- 1e3 
xs<-seq(xmin,xmax,length=n) 
ys<-seq(xmin,xmax,length=n) 

f = function(x, y) (x^2+0.7*y^2-1)^3 - x^2*y^3 
zs <- outer(xs,ys,FUN=f) 

h <- contourLines(xs,ys,zs,levels=0) 
library(txtplot) 
with(h[[1]], txtplot(x, y)) 



    +---+-******----+----******-+---+ 
1.5 + ***** **********  ***** + 
    1 +**       * + 
0.5 +**       * + 
    | ***      *** | 
    0 + ****     **** + 
-0.5 +  *****   *****  + 
    -1 +   ***********   + 
    +---+-----+-----*-----+-----+---+ 
     -1 -0.5 0 0.5 1  
+4

ASCII藝術如此復古。 –

6

幾個品種多:

equations

+0

我沒有低估這一點,但我認爲它是低估的,因爲它不過是一個參數方程組的鏈接,它直接或間接地通過其他答案鏈接到這裏。 –

2

我不知道關於R任何東西,但如果你繪製這個函數,你會得到一個心臟:

x^2+(y-(x^2)^(1/3))^2=1