animated plots in R with tweenR

animated plots in R with tweenRKirshnee BhagwandasBlockedUnblockFollowFollowingFeb 6Photo by Markus Spiske on UnsplashUtilizing animations to show trends in data can be a great storytelling tool.

There are a couple packages around for creating animation effects in RI recently came across tweenR , which allows you to create smoothed transitions by interpolation.

The tweenr package doesn’t actually perform any animation itself; rather it creates intermediate states, that makes the animation smoother.

Below is a tutorial using some dummy data:library(dplyr)library(scales)library(tweenr)library(animation)library(dplyr)library(ggplot2)library(ggthemes)# an example of a time series data frameplot_data <- data.

frame(Date =seq(as.

Date("2010/1/1"), as.

Date("2019/1/1"), "years"), Value=runif(10,0,1000)) # prepare data for tween by creating columns for the time between pointsdf.

all<-plot_data %>% mutate(day=as.

numeric(Date-min(Date)+1),ease="linear")# add rows to fill in the gapsplot_data_tween<-tween_elements(df.

all, time = "day", group="ease", ease="ease", nframes = nrow(df.

all)*20)df_tween_appear <- tween_appear(plot_data_tween, time='day', nframes = nrow(df.

all)*10)Function for making plot:make_plot <- function(i){ plot_data <- df_tween_appear %>% filter(.

frame==i, .

age> -3.

5) p<- plot_data %>% ggplot()+ geom_line(aes(x=Date, y=Value),color="darkgray", size=0.

7)+ geom_point(data=.

%>% filter(Date==max(Date)), mapping=aes(x=Date, y=Value), size=3,color="pink",stroke=4)+ geom_point(data=.

%>% filter(Date==max(Date)), mapping=aes(x=Date, y=Value), color="white", size=2)+ geom_text(data=.

%>% filter(Date==max(Date)), mapping=aes(x=Date, y=Value,label=round(Value,0)), color="black", nudge_x=7,hjust=1.

5, size=6)+ geom_line(data=df.

all, aes(x=Date,y=Value),alpha=0.

1)+ theme_minimal(base_family = "sans")+ scale_y_continuous(sec.

axis=dup_axis())+ theme(plot.

subtitle=element_text(face="italic",size=14), plot.

title=element_text(color="darkcyan",size=19), plot.

caption=element_text(hjust=0), panel.



x = element_line(color="lightgray"), panel.



x = element_line(color="lightgray"), panel.



y = element_line(color="lightgray"), panel.



y = element_line(color="lightgray"), axis.


x = element_text(angle = 90, hjust = 1, size=12))+ labs(x="",y="Total Value", caption="All values- 2010-2019.

")+ ggtitle(("Yearly Trends of Values")) return(p)}Now we loop through each frame and create the plots and then use the animation package’s saveGIF function to save it.


options(interval=1/10) # Number to set the time interval of the animation (unit in seconds); adjust for speed of anisaveGIF({for (i in 1:max(df_tween_appear$.

frame)){ g<-make_plot(i) print(g) print(paste(i,"out of",max(df_tween_appear$.

frame))) ani.

pause() # adds a pause the end of animation}},movie.



width = 500, ani.

height = 350)A fun example: TIME covers over the yearsI played around with Kaggle’s ‘Cover Pictures of Time Magazine’ data set.

The data contains the number of females and males featuring on the cover every year.

I then added frames and ease to the data frame.

Adjusting the function above ‘make_plot’ and then grouping by gender in the tween_elements parameter so that a frame is created per year.

The resulting plot is shown below:Demography of GenderI found some great recourses on animating in R, in particular : The tweenr github has some tutorials: https://github.

com/thomasp85/tweenrI used the great tutorials here to learn and understand tweenR more:http://lenkiefer.


. More details

Leave a Reply