Animated charts: visualizing “changes” in R

Animated charts: visualizing “changes” in RShing-Yun ChiangBlockedUnblockFollowFollowingApr 2MakeoverMonday released a weekly data challenge about a world development indicator from World Bank: adolescent fertility rate (births per 1,000 women age 15–19) in March.

With this dataset, at first I would like to see how the numbers change through time and if there are patterns in terms of income groups.

This article reflects the process of data manipulation and how the charts were developed from static to animated.

Changes in linesWhen talking about changes through time, a line chart is the quick and clear solution to see the results.

setting <- ggplot(final, aes(Year, Rate, group = Country))setting+ geom_line(alpha = 0.

5, aes(color = factor(Income_group))) + scale_colour_manual(values = c(high, upmiddle, lowmiddle, low)) + facet_grid(~Income_group) + theme_minimal()As I expected, the adolescent fertility rate is going down almost in every country.

The difference by income groups is that adolescent fertility rates in high-income countries dropped down pretty fast in 1990s; while the rates in countries with lower income go down slowly.

Things are getting better, they are just not that fast in some parts of the world.

Visual emphasis on “changes”With the observation above, I was thinking how to visualize the quick and slow changes by income groups.

I extracted the dataset from year 1960 to 2016 with 10-year intervals, and created histograms to see the changes by years.

# extract data for some yearsdata.

someyears <- final %>% filter(Year %in% c(2016, 2010, 2000, 1990, 1980, 1970, 1960))ggplot(data.

someyears, aes(Rate, fill = factor(Income_group))) + geom_histogram() + scale_fill_manual(values = c(high, upmiddle, lowmiddle, low)) + facet_grid( ~Year) + theme_minimal() + coord_flip()As the histograms show, more and more countries have lower adolescent fertility rates as time goes on.

High-income countries have a significant move from higher rates to lower; low-income countries only make a little progress.

However, it is not easy to imagine the movement from 8 separate slices of data.

Making animated chartsThe series of histograms remind me of the idea of making animated charts, so that we can “see” the changes directly.

I found a helpful article about making animation in R using gganimate library.

movinghist <- ggplot(final, aes(Rate, fill = factor(Income_group))) + geom_histogram() + scale_fill_manual(values = c(high, upmiddle, lowmiddle, low)) + theme_minimal() + coord_flip() + #animation setting labs( title = ‘Year: {frame_time}’, y = ‘Number of countries’, x = ‘Adolescent Fertility Rate (births per 1000 woman between 15–19)’ ) + transition_time(as.

integer(Year)) + #show the Year as integer ease_aes(‘linear’)animate(movinghist, width = 600, height = 600)The animation helps a lot.

We are able to see how countries in different income groups move toward lower adolescent fertility rates.

Aside from visualizing changes by groups, I went further to visualize changes for individual country.

In this way, each country has its own place, a dot is an adolescent fertility rate of one year.

Since there are so many countries, I made the animated charts one income group at a time.

Below is the results for a group:The moving dots display interesting behaviors.

Some countries have the rates go up and down.

Some have significant drops.

The chart will be more informative if color coded.

Therefore, I calculated the changes in number for each year and use green for decreases (because the lower, the better), red for increases, grey for no changes (the start year 1960 is also no change)movingdots <- ggplot(filter(final, Income_group == ‘High income’), aes(Country, Rate, color = factor(Status))) + geom_point(alpha = 0.

7, size = 2) + scale_color_manual(values = c(down, up, same)) + facet_grid(~Income_group, scales = ‘free’) + theme_minimal() + theme(axis.

text.

x = element_text(angle = 60, hjust = 1)) + #animation setting labs( title = ‘Year: {frame_time}’, x = ‘Country’, y = ‘Adolescent Fertility Rate (births per 1000 woman between 15–19)’ ) + transition_time(as.

integer(Year)) + #show the Year as integer shadow_mark(alpha = 0.

3, size = 0.

5) + ease_aes(‘linear’)animate(movingdots, width= 1800, height=600)I am kind of addicted to these dots, keep looking at them.

Feel free to share feedback with me.

.

. More details

Leave a Reply