R-Codes: R Programming-animation package
Animation Package Example:
Source: http://rforpublichealth.blogspot.com.tr/
R-Codes: R Programming animation package Example:
library(animation)
library(ggplot2)
library(colorspace)
bas<‐data.frame(x=rnorm(500, 0, 1), y=rnorm(500, 0, 1), z=rnorm(500,0,1))
sign<‐c("V","O","L","K","A","N","O","B","A","N","!!")
colors <‐ rainbow_hcl(10, c=300)
xcoord<‐rep(c(‐2, ‐1, 0, 1, 2),2)
ycoord<‐c(2, 1.7, 2.1, 1.5, 2, ‐.5, 0, ‐1, ‐.8, ‐.7)
theme.both<‐ theme(legend.position="none",
panel.background = element_blank(),
axis.ticks = element_blank(),
axis.line = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
plot.background = element_rect(fill = "black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank())
index<‐1
ggplot(happy2015, aes(x, y, alpha = z, color=z)) +
geom_point(alpha=0.2) + labs(title="", x="", y="") +
theme.both +
scale_colour_gradient(low = "white", high="lightblue")+
annotate("text", x=xcoord[index], y=ycoord[index], size=15, label=sign[index], color=colors[index])
draw.a.plot<‐ function(index){
#make up a new dataframe
bas<‐data.frame(x=rnorm(500, 0, 1.5), y=rnorm(500, 0, 1.5), z=rnorm(500,0,1.5))
#plot according to the index passed
g<‐ggplot(happy2015, aes(x, y, alpha = z, color=z)) +
geom_point(alpha=0.2) + labs(title="", x="", y="") +
theme.both +
scale_colour_gradient(low = "white", high="lightblue")+
annotate("text", x=xcoord[index], y=ycoord[index], size=15, label=sign[index], color=colors[index])
#print out the plot
print(g)
}
#set up function to loop through the draw.a.plot() function
loop.animate <‐ function() {
lapply(1:length(sign), function(i) {
draw.a.plot(i)
})
}
saveGIF(loop.animate(), interval = .5, movie.name="bas.gif")

Animaton Package in R An Example:

  • 1.
    R-Codes: R Programming-animationpackage Animation Package Example: Source: http://rforpublichealth.blogspot.com.tr/
  • 3.
    R-Codes: R Programminganimation package Example: library(animation) library(ggplot2) library(colorspace) bas<‐data.frame(x=rnorm(500, 0, 1), y=rnorm(500, 0, 1), z=rnorm(500,0,1)) sign<‐c("V","O","L","K","A","N","O","B","A","N","!!") colors <‐ rainbow_hcl(10, c=300) xcoord<‐rep(c(‐2, ‐1, 0, 1, 2),2) ycoord<‐c(2, 1.7, 2.1, 1.5, 2, ‐.5, 0, ‐1, ‐.8, ‐.7) theme.both<‐ theme(legend.position="none", panel.background = element_blank(), axis.ticks = element_blank(), axis.line = element_blank(), axis.text.x = element_blank(), axis.text.y = element_blank(), plot.background = element_rect(fill = "black"), panel.grid.major = element_blank(), panel.grid.minor = element_blank())
  • 4.
    index<‐1 ggplot(happy2015, aes(x, y,alpha = z, color=z)) + geom_point(alpha=0.2) + labs(title="", x="", y="") + theme.both + scale_colour_gradient(low = "white", high="lightblue")+ annotate("text", x=xcoord[index], y=ycoord[index], size=15, label=sign[index], color=colors[index]) draw.a.plot<‐ function(index){ #make up a new dataframe bas<‐data.frame(x=rnorm(500, 0, 1.5), y=rnorm(500, 0, 1.5), z=rnorm(500,0,1.5)) #plot according to the index passed g<‐ggplot(happy2015, aes(x, y, alpha = z, color=z)) + geom_point(alpha=0.2) + labs(title="", x="", y="") + theme.both + scale_colour_gradient(low = "white", high="lightblue")+ annotate("text", x=xcoord[index], y=ycoord[index], size=15, label=sign[index], color=colors[index]) #print out the plot print(g) }
  • 5.
    #set up functionto loop through the draw.a.plot() function loop.animate <‐ function() { lapply(1:length(sign), function(i) { draw.a.plot(i) }) } saveGIF(loop.animate(), interval = .5, movie.name="bas.gif")