Animating Your Data Visualizations Like a Boss Using R

Each frame is a different plot when conveying motion, which is built using some relevant subset of the aggregate data..The subset drives the flow of the animation when stitched back together.How Animation is Done For Data VisualizationsThe more subsets, the smoother your plot will be..The motion of an animated plot is ‘driven’ by a variable in the data set..Again all the source code to create these visualizations can be found in the MatrixDS Project linked here.The PackagesThere are four package options I typically use for animating data in R:animate: can be used to animate any plot type, written by Yihui Xiegganimate: used to specifically animate ggplot graphics, written by Thomas Lin Pedersenplotly: an interactive plotting library which has animation featuresgooglevis: has a flash based motion chart optionGenerally speaking, the animate package provides the most control and is great for base plots but can be verbose..On the other hand, the gganimate package is limited to one plotting library but makes building motion into ggplot extremely easy (one or two extra lines of code in your plot)..Similar to gganimate, plotly can only animate visualizations from its own library..For this reason, I will not do an example of this library, take a look here if you have flash installed.Using animateWith the animate package, you have five options of exporting, assuming you have the proper dependencies installed (see documentation).HTML (with controls)Video (mp4)GIFLaTeXFlashThis package is great, in part, because it has some amazing statistical graphics built in as examples..For example, the good and bad animations above where built using this library and ggplot (see the MatrixDS project).Using gganimateWith gganimate, the default export is a GIF (using gifski if you must have rust installed) but there are also other options depending on your specific requirements..The first animation example in this article is built using the gganimate package (indeed it is the only complete real data example in the package’s README)..Here is an example animation from the plotly docs (using the frame argument) of the same gapminder dataset we saw with the gganimate package:library(plotly)library(gapminder)p <- gapminder %>% plot_ly( x = ~gdpPercap, y = ~lifeExp, size = ~pop, color = ~continent, frame = ~year, text = ~country, hoverinfo = "text", type = 'scatter', mode = 'markers' ) %>% layout( xaxis = list( type = "log" ) )Once you have the plot you can push it to the plotly service for hosting.. More details

Leave a Reply