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:
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:
Leave a comment