Animated bar charts that can be embedded directly into a post on any website are becoming more and more popular. They display the dynamics of changes in any characteristics over a certain time and do it visually. Let's see how to create them using R and generic packages.
These two are essential. In addition, tidyverse, janitor, and scales will be required for data manipulation, array cleanup, and formatting, respectively.
Data
The original dataset that we will be using in this project is downloaded from the World Bank website. Here they are - World Bank Data. The same data, if you need it ready-made, can be downloaded from project folders.
What is this information? The sample contains the value of the GDP of most countries for several years (from 2000 to 2017).
Data processing
We will use the code below to prepare the required data format. We clean up the column names, turn the numbers into a number format, and convert the data using the gather() function. Everything that is received is saved in gdp_tidy.csv for further use.
Building a complete set of actual histograms using ggplot2.
Animate static histograms with desired parameters using gganimate.
The final step is to render the animation in the desired format, including GIF or MP4.
Loading Libraries
library(tidyverse)
library(ganimate)
Data management
In this step, you need to filter the data to get the top 10 countries each year. Let's add some columns that will allow us to display the legend for the histogram.
gdp_tidy <- read_csv("./data/gdp_tidy.csv")
gdp_formatted <- gdp_tidy %>%
group_by(year) %>%
# The * 1 makes it possible to have non-integer ranks while sliding
mutate(rank = rank(-value),
Value_rel = value/value[rank==1],
Value_lbl = paste0(" ",round(value/1e9))) %>%
group_by(country_name) %>%
filter(rank <=10) %>%
ungroup()
Building static histograms
Now that we have a batch of data in the right format, we start drawing static histograms. Basic information - top 10 countries with the maximum GDP for the selected time interval. Graphs are built for each year.
Plotting with ggplot2 is very easy. As you can see in the code snippet above, there are several key points with the theme() function. They are necessary so that all elements animate without problems. Some of them may not be displayed if necessary. Example: Only the vertical gridlines and legends are drawn, but the axis titles and a few other components are removed from the lot.
ΠΠ½ΠΈΠΌΠ°ΡΠΈΡ
The key function here is transition_states(), which glues separate static graphs together. view_follow() is used to draw grid lines.
anim = staticplot + transition_states(year, transition_length = 4, state_length = 1) +
view_follow(fixed_x = TRUE) +
labs(title = 'GDP per Year : {closest_state}',
subtitle = "Top 10 Countries",
caption = "GDP in Billions USD | Data Source: World Bank Data")
Rendering
After the animation is created and stored in the anim object, it's time to render it using the animate() function. The renderer used in animate() may be different depending on the type of output file required.