Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
generate heart plot for valentine's day
require(ggplot2)
t <- seq(0,2*pi, by=0.1)
x <- 16*sin(t)^3
y <- 13*cos(t) - 5*cos(2*t) - 2*cos(3*t) - cos(4*t)
d <- data.frame(x=x,y=y, f=0)
a <- x
b <- y
for (i in 1:30) {
a <- a * 0.9
b <- b * 0.9
dd <- data.frame(x=a,y=b, f=i)
d <- rbind(d, dd)
}
p <- ggplot(d, aes(x,y, group=factor(f))) +
geom_path(aes(color="hotpink")) +
opts(legend.position="none") +
xlab("") + ylab("")
rotate <- function(mat, degree) {
## reference
## http://en.wikipedia.org/wiki/Rotation_matrix
mat <- as.matrix(mat)
theta <- degree/180 * pi
r <- matrix(c(cos(theta), -sin(theta),
sin(theta), cos(theta)),
byrow=T, ncol=2)
mat <- (mat-colMeans(mat)) %*% r
return(mat)
}
xy <- matrix(c(x,y), byrow=F, ncol=2)
xy2 <- rotate(xy, -30)
xy3 <- rotate(xy*0.7, 15)
d1 <- data.frame(x=xy2[,1], y=xy2[,2],f=1)
d2 <- data.frame(x=16+xy3[,1], y=xy3[,2]-3,f=2)
dfr <- rbind(d1,d2)
g <- ggplot(dfr, aes(x,y,
group=factor(f),
color=factor(f))) +
geom_path() +
geom_text(aes(x=13, y=10,
label="Happy Valentine's Day!",
colour="black",angle=-30)) +
scale_color_manual(values=c("red",
"hotpink", "black")) +
opts(legend.position="none") +
xlab("") + ylab("")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.