gganimate converts any ggplot2 visualization into a ggplot2 animation. I have already published various posts containing gganimate animations. For R programmers or analysts with proficiency in R this is a very interesting package. See e.g. my blog post introducing comtradr for international trade analysis (containing an animation created with gganimate) and my post containing spatial data animations of spatial specimen spread. Or you could check my monte-carlo animation to animate stock price risk with gganimate.
- Link: Animating commodity price developments with comtradr and gganimate in R
- Link: Spatial data animations with gganimate in R
- Link: Monte-carlo animation in R with gganimate
gganimate is compatible with ggplot2. Since ggmap is basically an extended form of ggplot2 gganimate also works with the ggmap-package.
In this article I want to demonstrate how you can create ggplot2 animations with histograms. I will work with artificially produced data, i.e. manufactured data fit-for-purpose.
Setting up data for creating a ggplot2 animation
In a first example I create an artificial data set that contains an observations value (e.g. age at marriage, age of parents at birth, life expectancy or other) that is randomly distributed around a mean. I will let this distribution “change” over time.
To be precise, I will use random normal distribution with a defined mean and a standard deviation that is slightly increasing year by year. I will consider 21 years, starting in 2000 and ending in year 2020 (last observation from 2020). In each year, 1,000 data points were collected for the fictive variable being observed.
Below I set up the artificial dataset.
# data set
dataset = as.data.frame(matrix(nrow=21000,ncol=2))
# column names
colnames(dataset) = c("year","obsVal")
# create artificial data
for(i in 2000:2020){
for(j in 1:1000){
obsval = rnorm(n=1,mean=30,sd=3+(i-1999)/10)[1]
dataset$year[(i-2000)*1000 + j] = i
dataset$obsVal[(i-2000)*1000 + j] = obsval
}
}
# view dataset header
head(dataset)
year<int> | obsVal<dbl> | |
---|---|---|
1 | 2000 | 28.92808 |
2 | 2000 | 26.43280 |
3 | 2000 | 27.87782 |
4 | 2000 | 27.17254 |
5 | 2000 | 27.91719 |
6 | 2000 | 29.11332 |
The spread in data for an observation variable, such as the one measured with observation values here, can e.g. be explored using a histogram.
ggplot2 animation of histogram with gganimate
I will create an animation with gganinate that steps through the histograms for each year, i.e. depicts one histogram for each year. The trick of doing so is to first create a histogram in ggplot2, for every year.
# import relevant packages
library(gganimate)
## Loading required package: ggplot2
library(ggplot2)
# create ggplot2 plot
ggplot(dataset) +
geom_histogram(mapping=aes(y=obsVal,group=year),bins=100,col="red",fill="red") +
ggtitle("Distribution of observation values") +
labs(subtitle=("Year: {closest_state}")) +
xlab("Absolute frequency") +
ylab("Observation value [-]") +
transition_states(year, transition_length = 3, state_length = 1)
# save animation as GIF
anim_save("histograms.gif")
As some prefer histograms to point skywards, I can also rotate the histogram by 90 degrees:
# create ggplot2 plot
ggplot(dataset) +
geom_histogram(mapping=aes(x=obsVal,group=year),bins=100,col="red",fill="red") +
ggtitle("Distribution of observation values") +
labs(subtitle=("Year: {closest_state}")) +
ylab("Absolute frequency") +
xlab("Observation value [-]") +
transition_states(year, transition_length = 3, state_length = 1)
# save ggplot2 animation as GIF (gganimate)
anim_save("histogram90.gif")
Summary and conclusion
In this article I implement a simple ggplot2 animation with a histogram in gganimate. I showed how you can animate a ggplot2 visualization using gganimate and how to modify the visualization as well as the animation. In other referenced articles (see my list in the introduction of this post) I share additional examples and use cases related to ggplot2 animations with gganimate.
Data scientist focusing on simulation, optimization and modeling in R, SQL, VBA and Python
Leave a Reply