Making an Animated Heart GIF in R

[This article was first published on Data, Evidence, and Policy - Jared Knowles, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Messing around with animations, I created this toy example to make a growing outline of a heart in R. I have put it up as a gist on GitHub and embedded it below. It uses the animation and the ggplot2 packages–both of which make it incredibly simple to make a good looking graphic.

 The formula to draw the hearts can be found thanks to Wolfram MathWorld.

 

############################################################
## Title: Make an Animated Heart in R
## Author: Jared Knowles
## Date: May 5th, 2012
############################################################
# Make heart curve
t<-seq(-100,100,length.out=1000) # Order
x<-16*sin(t)^3 # Create the Xs from a formula
y<-(13*cos(t))-(5*cos(2*t))-(2*cos(3*t))-(cos(4*t)) # Create Ys from a formula
# These use non-polar coordinates and can be specified algebraically, which is nice.
#For more detail, look here: http://mathworld.wolfram.com/HeartCurve.html
heart<-data.frame(x=x,y=y,order=t) # make a data frame for ggplot2 plotting
library(animation) # We need the excellent animation package
library(ggplot2 # And ggplot2
ani.options(outdir=getwd(),imgdir=getwd(),tempdir=getwd()) # Important on Windows 7
# to avoid unwritable tempfile
#Maybe can ignore on Mac or Linux
####################
# Animation Loop
###################
saveGIF({
for(i in seq(0,1,by=.05)) # Set number of iterations, i is the scale factor for the heart
print(qplot(i*x,i*y,geom='path',data=heart,color=I('red'),order=t,size=I(1.2))+
# Need print statement in saveGIF
xlim(c(-16,16))+ylim(c(-17,12))+xlab('')+ylab('')+ # Remove pesky axis text, avoid resizing plot area
theme_bw()+opts(axis.text.x=theme_blank(),axis.ticks=theme_blank(),axis.text.y=theme_blank(),
# clean the theme
title='Growing Heart'))
# add a title
},movie.name="img/heart.gif",interval=0.1,nmax=30,ani.width=600,ani.height=600,convert='gm convert')
# set animation size, and other options
# Note you need to have GraphicsMagick or ImageMagick installed.
# GraphicsMagick is specified here in the 'convert'
# option. You can also use IM, by removing the convert argument
view raw animatedheaRt hosted with ❤ by GitHub

To leave a comment for the author, please follow the link and comment on their blog: Data, Evidence, and Policy - Jared Knowles.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)