If only love were so simple

In case you’ve ever wondered how to graph a heart in R, I’ve figured it out for you. No need to thank me. Pink Heart Graph R

Maybe it could be featured on a nerdy valentine?

The Equations

Heart Graph Equations






The R Code

###Filled Heart
##Plot the top
  top <- function(x) (1-(abs(x)-1)^2)^.5
  pink <- rgb(201, 6, 45, max = 255)   #set color
  plot(top, xlim=c(-2,2), ylim=c(-3,1),
   ylab="y", col=pink, main="Heart")
##Plot the v
  v <- function(x) -3*(1-(abs(x)/2)^.5)^.5 
  plot(v, xlim=c(-2,2), ylim=c(-3,1),
    ylab="y", col=pink)
##Fill the Graph
  top.x <- seq(-2,2, .01)   #x coordinates for top
  v.x <- seq(-2,2, .01)     #x coordinates for v 
  top.y <- top(v.x)        #y coordinates for top
  v.y <- v(v.x)            #y coordinates for v
  x <- c(v.x, rev(top.x))    #put the coordinates together
  y <- c(v.y, rev(top.y))
  polygon(x, y, col=pink, border=pink)


4 thoughts on “If only love were so simple

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s