Possible Duplicate:
Equation-driven smoothly shaded concentric shapes
How could I plot a symmetrical heart in R like I plot a circle (using plotrix) or a rectangle?
I'd like code for this so that I could actually do it for my self and to be able to generalize this to similar future needs. I've seen even more elaborate plots than this so it's pretty doable, it's just that I lack the knowledge to do it.
This is an example of plotting a "parametric equation", i.e. a pairing of two separate equations for x and y that share a common parameter. You can find many common curves and shapes that can be written within such a framework.
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
You also could "heat it up" with using the "fill" capability of the polygon
function:
with(dat, polygon(x,y, col="hotpink"))
And if you just want little hearts to sprinkle around at various places, you could use the Symbol font version of "heart" after looking at the help(points)
page and using the TestChars
function:
points(c(10,-10, -15, 15), c(-10, -10, 10, 10), pch=169, font=5)
Windows users may want to see if adding the Cairo package helps gain access to card symbols including "hearts".( When I tested the TestChars function on the WinXP "side" of my MacPro I did not get hearts, and paging through the "special symbols" in MS-Word did not uncover anything. So I did a search of Rhelp and found a recent posting by Ivo Welch. He was reporting a bug, but they look OK on my machine.) Further note... I think the hearts and diamonds codes in his were reversed.
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') ) )
From a blog post:
Solve the parametric equation for y (does SO allow math formatting?)
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)
Simple and ugly hack:
plot(1, 1, pch = "♥", cex = 20, xlab = "", ylab = "", col = "firebrick3")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With