Skip to content

Instantly share code, notes, and snippets.

@AABoyles
Forked from jknowles/animatedheaRt
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AABoyles/9004943 to your computer and use it in GitHub Desktop.
Save AABoyles/9004943 to your computer and use it in GitHub Desktop.
Animated Beating Heart in R
############################################################
## Title: Animated Beating Heart in R
## Author: Jared Knowles
## Forked: Tony Boyles
## Date: February 14, 2014
############################################################
library(animation)
library(ggplot2)
t<-seq(-100,100,length.out=2010)
x<-16*sin(t)^3
y<-(13*cos(t))-(5*cos(2*t))-(2*cos(3*t))-(cos(4*t))
heart<-data.frame(x=x,y=y,order=t)
if(!file.exists("heart.gif")){
file.create("heart.gif")
}
ani.options(outdir=getwd(),imgdir=getwd(),tempdir=getwd())
saveGIF({
for(i in c(seq(0.85,1.00,by=.01),seq(0.99,0.85,by=-.01),seq(0.86,0.91,by=.01),seq(.90,0.86,by=-.01)))
print(
ggplot(heart, aes(i*x,i*y)) +
geom_path(color="red", size=5)+
xlim(-16,16)+
ylim(-17,12)+
labs(x='', y='')+
theme_bw()+
theme(
axis.text.x=element_blank(),
axis.ticks=element_blank(),
axis.text.y=element_blank())
)
},movie.name="heart.gif",interval=0.03,ani.width=300,ani.height=300,convert='convert')
@AABoyles
Copy link
Author

If you can't see it beating, that's because most webservers will serve gifs as jpgs, which causes the browser to view them as static. Try running the R Code to create your own.

@AABoyles
Copy link
Author

...or download the raw version and open it in an image previewer...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment