Created
May 5, 2012 21:41
-
-
Save jknowles/2605771 to your computer and use it in GitHub Desktop.
An Animated Heart in R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################################################ | |
## 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 | |
This is indeed very old code. I think it might work if you replace the opts
function call with theme
which is the new way these options are handled in ggplot2. That said I'm not sure qplot()
is still supported so you still may run into issues.
Many thanks for the prompt feedback, especially a decade after you posted the code. Was not expecting a reply to be honest!!!. Will try the suggestion!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I know this is an old code but, there is certainly a parenthesis missing in line 18 and even if I correct that I end up with an error: s(axis.text.x = theme_blank(), axis.ticks = theme_blank(), :
could not find function "opts"
In addition: Warning message:
In geom_path(color = "red", order = c(-100, -99.7997997997998, -99.5995995995996, :
Ignoring unknown parameters:
order
animation option 'nmax' changed: 30 --> 50