Valentine’s Day with R

Playing with ggplot2 library in R and produce some handy things useful on Valentine’s day.

Flower:

library(ggplot2)
Valentine =function(x) 1-cos(x)

opt=theme(legend.position="none",
panel.background = element_rect(fill="white"),
panel.grid = element_blank(),
axis.ticks=element_blank(),
axis.title=element_blank(),
axis.text =element_blank()
)

ggplot(data.frame(x = c(2, 10*pi)), aes(x)) +
stat_function(fun=Valentine, geom="density", fill="red", colour="red") +
coord_cartesian(start=-pi) +
opt

And you will get a flower:

Rplot_flower

Writing heart is fairly simple using 1-cos(x) function for polar equation (and not Cartesian equation as in previous example).

library(ggplot2)
Valentine=function(x) 1-cos(x)

opt=theme(legend.position="none",
panel.background = element_rect(fill="white"),
panel.grid = element_blank(),
axis.ticks=element_blank(),
axis.title=element_blank(),
axis.text =element_blank()
)

ggplot(data.frame(x = c(3, 3*pi)), aes(x)) +
stat_function(fun=Valentine, geom="density", fill="red", colour="red") +
geom_text(x=-.55, y=1, colour="red", label="Happy Valentine's Day", size=12)+
coord_polar(start=-pi) +
opt

The result should look like this:

Rplot_heart

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s