在R中绘制一颗心

Tyl*_*ker 39 r parametric-equations polar-coordinates

可能重复:
方程驱动的平滑阴影同心形状

我怎么能在R中绘制一个对称的心脏,就像我绘制一个圆圈(使用plotrix)或一个矩​​形?

我想要这样的代码,这样我才能真正为自己做这件事,并能够将其推广到类似的未来需求.我已经看到了比这更精细的情节所以它非常可行,只是因为我缺乏这方面的知识.

42-*_*42- 81

这是绘制"参数方程"的示例,即,共享共同参数的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"))
Run Code Online (Sandbox Code Playgroud)

其他参数(和隐式和极性)心脏方程

您还可以使用该功能的"填充"功能"加热" polygon:

with(dat, polygon(x,y, col="hotpink"))   
Run Code Online (Sandbox Code Playgroud)

如果你只想在不同的地方撒上小小的心脏,你可以在查看help(points)页面并使用TestChars函数后使用Symbol字体版本的"heart" :

points(c(10,-10, -15, 15), c(-10, -10, 10, 10), pch=169, font=5)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Windows用户可能想看看添加Cairo软件包是否有助于访问卡片符号,包括"心脏".(当我在MacPro的WinXP"侧"测试TestChars功能时,我没有得到心灵,并且通过"特殊"分页符号"在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') ) )
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


aL3*_*3xa 17

简单而丑陋的黑客:

plot(1, 1, pch = "?", cex = 20, xlab = "", ylab = "", col = "firebrick3")
Run Code Online (Sandbox Code Playgroud)

  • 但如果像心脏一样走路,像心脏一样嘎嘎叫,那一定是一颗心.:) (5认同)
  • 但如果想用心作为绘图人物,这实际上可能是一个非常好的起点:见http://stackoverflow.com/questions/5886018/using-unicode-dingbat-like-glyphs-in-r-graphics-across -devices-platforms-e和http://www.fileformat.info/info/unicode/char/search.htm?q=heart&preview=entity (2认同)

Ben*_*ker 17

来自博文:

求解y的参数方程(SO是否允许数学格式化?)

x ^ 2 +(5y/4-sqrt(abs(x)))^ 2 = 1

sqrt(1-x ^ 2)= 5y/4 - sqrt(abs(x))

y = 4/5*(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)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


And*_*rie 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()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

心脏情节(由@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)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


bap*_*ste 9

另外一个选项,

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    
Run Code Online (Sandbox Code Playgroud)

  • ASCII艺术是如此复古. (4认同)

Car*_*oft 5

如果您想变得更“成熟”,请尝试以下方法(几年前发布到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)  
}
Run Code Online (Sandbox Code Playgroud)