{"id":495,"date":"2014-01-31T10:21:12","date_gmt":"2014-01-31T15:21:12","guid":{"rendered":"http:\/\/homepages.uc.edu\/~yaozo\/wordpress\/?p=495"},"modified":"2014-01-31T10:21:12","modified_gmt":"2014-01-31T15:21:12","slug":"animated-plots-in-r-and-latex","status":"publish","type":"post","link":"https:\/\/zhuoyao.net\/index.php\/2014\/01\/31\/animated-plots-in-r-and-latex\/","title":{"rendered":"Animated plots in R and LaTeX"},"content":{"rendered":"<div>Published on\u00a0<a href=\"http:\/\/robjhyndman.com\/hyndsight\/animations\/\">13 October 2010<\/a><\/div>\n<div>\n<p>I like to use ani\u00admated plots in my\u00a0<a href=\"http:\/\/robjhyndman.com\/?s=functional+talks\">talks on func\u00adtional time series<\/a>, partly because it is the only way to really see what is going on with changes in the shapes of curves over time, and also because audi\u00adences love them! Here is how it is\u00a0done.<\/p>\n<p>For LaTeX, you need to cre\u00adate every frame as a\u00a0sep\u00ada\u00adrate graph\u00adics file. Here is an exam\u00adple. First the R\u00a0code:<\/p>\n<div>\n<table>\n<tbody>\n<tr id=\"p10501\">\n<td id=\"p1050code1\">\n<pre>library(demography)\nnyears &lt;- length(fr.mort$year)\nfor(i in 1:nyears)\n{\n    pdf(paste(\"frmale\",i,\".pdf\",sep=\"\"),height=4,width=6.5)\n    x &lt;- fr.mort\n    if(i&lt;nyears)\n        x$rate$male[,(i+1):nyears] &lt;- NA\n    plot(x,series=\"male\",ylim=c(-9.5,1.5),\n        main=paste(\"French: male mortality (\",fr.mort$year[1]-1+i,\")\",sep=\"\"))\n    if(i&gt;1)\n        x$rate$male[,1:(i-1)] &lt;- NA\n    lines(x,series='male',lwd=2,col=1)\n    dev.off()\n}<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>This cre\u00adates a\u00a0series of pdf files in the figs direc\u00adtory, named frmale1.pdf, \u2026, frmale191.pdf. \u00a0In the LaTeX file, you need to load the\u00a0<a href=\"http:\/\/www.ctan.org\/tex-archive\/macros\/latex\/contrib\/animate\/\">ani\u00admate pack\u00adage<\/a>. Then the fol\u00adlow\u00ading com\u00admand will do the\u00a0magic:<\/p>\n<div>\n<table>\n<tbody>\n<tr id=\"p10502\">\n<td id=\"p1050code2\">\n<pre>\\centerline{\\animategraphics[controls,buttonsize=0.3cm,width=12.5cm]\n    {6}{\"frmale\"}{1}{191}}<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>(Put it all on one line with\u00adout the spaces.) This is how the graph on slide 2\u00a0of\u00a0<a href=\"http:\/\/robjhyndman.com\/talks\/DemographicForecasting.pdf\">this pre\u00adsen\u00adta\u00adtion<\/a>\u00a0was produced.<\/p>\n<p>For web usage, it is bet\u00adter to pro\u00adduce an ani\u00admated gif ver\u00adsion in\u00a0R:<\/p>\n<div>\n<table>\n<tbody>\n<tr id=\"p10503\">\n<td id=\"p1050code3\">\n<pre>library(animation)\nlibrary(demography)\nnyears &lt;- length(fr.mort$year)\nmakeplot &lt;- function(){\nfor(i in 1:nyears)\n{\n    x &lt;- fr.mort\n    if(i&lt;nyears)\n        x$rate$male[,(i+1):nyears] &lt;- NA\n    plot(x,series=\"male\",ylim=c(-9.5,1.5),\n        main=paste(\"French: male mortality (\",fr.mort$year[1]-1+i,\")\",sep=\"\"))\n    if(i&gt;1)\n        x$rate$male[,1:(i-1)] &lt;- NA\n    lines(x,series='male',lwd=2,col=1)\n}\n}\noopt = ani.options(interval = 0, nmax = nyears)\nsaveMovie(makeplot(),interval = 0.1, width = 580, height = 400)\nani.options(oopt)<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>Click the graph below for the ani\u00admated version.<\/p>\n<p><a title=\"Animated plots in R and LaTeX\" href=\"http:\/\/robjhyndman.com\/Rfiles\/animation\/animation.gif\" rel=\"lightbox[1050]\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" src=\"http:\/\/robjhyndman.com\/Rfiles\/animation\/frmale191.jpg\" width=\"580\" height=\"400\" \/><\/a><\/p>\n<p>For an expla\u00adna\u00adtion of the colours, see my\u00a0<a href=\"http:\/\/robjhyndman.com\/papers\/rainbow-fda\/\">rain\u00adbow plot paper<\/a>.<\/p>\n<p>The\u00a0<a href=\"http:\/\/cran.r-project.org\/web\/packages\/animation\/index.html\">ani\u00adma\u00adtion pack\u00adage for R<\/a>\u00a0also allows graph\u00adics to be saved in other for\u00admats. See<a href=\"http:\/\/animation.yihui.name\/\">Ani\u00adWiki<\/a>\u00a0for some exam\u00adples of ani\u00adma\u00adtions.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Published on\u00a013 October 2010 I like to use ani\u00admated plots in my\u00a0talks on func\u00adtional time series, partly because it is the only way to really&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"class_list":["post-495","post","type-post","status-publish","format-standard","hentry","category-r"],"_links":{"self":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts\/495","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/comments?post=495"}],"version-history":[{"count":0,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts\/495\/revisions"}],"wp:attachment":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/media?parent=495"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/categories?post=495"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/tags?post=495"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}