# Animating ggplot2 histograms with gganimate

I have already published various posts containing gganimate animations. For R programmers or analysts with profciency in R this is a very interesting package. It converts ggplot2-plots into animations. 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.

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 animate ggplot2 histograms with gganimate in R. I will work with artificially produced data, i.e. manufactured data fit-for-purpose.

## Creating artificial data to obtain a working dataset

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
}
}

6 rows

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.

## Animation of time-dependent distributions: Histogram over time

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")``````

## Rotating direction of ggplot2 histogram

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 animation as GIF
anim_save("histogram90.gif")``````