This is a quick sample of some of the charts that can be built in R.
The majority of the code is courtesy of Tristan Ganry’s article, https://towardsdatascience.com/how-to-build-animated-charts-like-hans-rosling-doing-it-all-in-r-570efc6ba382 with some small modifications. The data comes from https://www.gapminder.org/data. Gapminder is a non-profit organisation that produces free teaching resources to make the world understandable using reliable statistics.

Import Data

The first step is to download the data which is avaiable in Google Spreadsheet format. First, check if devtools is already installed and if not install it. Then install tibble and the github version of flipAPI to enable downloading Google Spreadsheets.
After installing check the first 10 rows of the first imported Google Spreadsheet with the head() function.

#devtools is required to install flipAPI
if(!require("devtools")) install.packages("devtools")
library(devtools)

#tibble is required to install fileAPI. Check the 
if(!require("tibble")) install.packages("tibble")
library(tibble)

#flipAPI can be used to download and read Excel files 
if(!require ("flipAPI")) devtools::install_github("Displayr/flipAPI")
library(flipAPI)

#Download the 'Population, total' spreadsheet (https://docs.google.com/spreadsheets/d/1IbDM8z5XicMIXgr93FPwjgwoTTKMuyLfzU6cQrGZzH8/pub) and save as the dataframe population_xls
population_xls <- DownloadXLSX("https://docs.google.com/spreadsheet/pub?key=phAwcNAVuyj0XOoBL_n5tAQ&output=xlsx", sheet = 1, want.data.frame = TRUE, want.factors = FALSE, want.col.names = TRUE)

#Download the 'Children per woman (total fertility)' spreadsheet (https://docs.google.com/spreadsheets/d/1oq3r8W7ajenKFgoAYoOf2MXeTWWNPpudR-Fo5m2-o30/pub) and save as the dataframe fertility_xls 
fertility_xls <- DownloadXLSX("https://docs.google.com/spreadsheet/pub?key=phAwcNAVuyj0TAlJeCEzcGQ&output=xlsx", sheet = 1, want.data.frame = TRUE, want.factors = FALSE, want.col.names = TRUE)

#Download the 'Life expectancy (years)' spreadsheet (https://docs.google.com/spreadsheets/d/1H3nzTwbn8z4lJ5gJ_WfDgCeGEXK3PVGcNjQ_U5og8eo/pub) and save as the dataframe lifeexp_xls 
lifeexp_xls <- DownloadXLSX("https://docs.google.com/spreadsheet/pub?key=phAwcNAVuyj2tPLxKvvnNPA&output=xlsx", sheet = 1, want.data.frame = TRUE, want.factors = FALSE, want.col.names = TRUE)

head(population_xls, 10)

Clean up the data

Check if dplyr is installed and if not install it. Dplyr is used to modify and clean up the imported data.

Filter each of the data frames to take the first column and the yearly data from 1950 to 2015. Rename the first column to “Country” and filter out any empty rows Check the first 10 rows of population dataframe.

if(!require ("dplyr")) install.packages("dplyr")
library(dplyr)

population <- population_xls %>% select("country" = "Total.population", X1900.0:X2015.0) %>% filter(!is.na(country))
fertility <- fertility_xls %>% select("country" = "Total.fertility.rate", X1900.0:X2015.0) %>% filter(!is.na(country))
lifeexp <- lifeexp_xls%>% select("country" = "Life.expectancy", X1900.0:X2015.0) %>% filter(!is.na(country))

head(population, 10)

Install reshape to reshape the dataframes from a wide format to narrow where the year columns are turned into a value. Then combine the three data frames into the dataframe allData using the merge() function.

# Use reshape library to move the year dimension as a column
if(!require("reshape")) install.packages("reshape")
library(reshape)

population_m <- melt(population, id=c("country")) 
lifeexp_m <- melt(lifeexp, id=c("country")) 
fertility_m <- melt(fertility, id=c("country")) 

# Rename the 3rd column (value) in each dataframe to the KPI name (e.g. population, lifeExpectancy, fertility)
colnames(population_m)[3] <- "population"
colnames(lifeexp_m)[3] <- "lifeExpectancy"
colnames(fertility_m)[3] <- "fertility"

#Merge the 3 dataframes into 1 new dataframe allData
allData <- merge(lifeexp_m, fertility_m, by=c("country","variable"), header =T)
allData <- merge(allData, population_m, by=c("country","variable"), header =T)
allData <- dplyr::rename(allData, year = variable)

head(allData, 10)

Add the Continent name to the allData data frame taking the continent name from the Gapminder library.

if(!require ("gapminder")) install.packages("gapminder")
library(gapminder)

#Get a list of distinct countries and their continents
continent <- gapminder %>% group_by(continent, country) %>% distinct(country, continent)

#Merge the continent value with the allData dataframe
allData <- merge(allData, continent, by = c("country"))

head(allData, 10)

Filter out all of the data where the value is empty. Check the structure with the str() function.

allData[is.na(allData)] <- 0
str(allData)
## 'data.frame':    9869 obs. of  6 variables:
##  $ country       : chr  "Afghanistan" "Afghanistan" "Afghanistan" "Afghanistan" ...
##  $ year          : Factor w/ 116 levels "X1900.0","X1901.0",..: 63 113 78 106 102 64 111 105 107 101 ...
##  $ lifeExpectancy: num  33 54.4 40.5 52 50.4 ...
##  $ fertility     : num  7.67 5.14 7.67 6.93 7.62 7.67 5.66 7.14 6.7 7.73 ...
##  $ population    : num  9343772 29726803 13056499 24399948 20531160 ...
##  $ continent     : Factor w/ 5 levels "Africa","Americas",..: 3 3 3 3 3 3 3 3 3 3 ...

Remove the factors. Then remove x from the year variable and change it to an integer. recheck the structure.

allData$year      <- as.character(allData$year)
allData$continent <- as.character(allData$continent)
allData$country   <- as.character(allData$country)

allData$year      <- as.integer(as.character(gsub("X","",allData$year)))
str(allData)
## 'data.frame':    9869 obs. of  6 variables:
##  $ country       : chr  "Afghanistan" "Afghanistan" "Afghanistan" "Afghanistan" ...
##  $ year          : int  1962 2012 1977 2005 2001 1963 2010 2004 2006 2000 ...
##  $ lifeExpectancy: num  33 54.4 40.5 52 50.4 ...
##  $ fertility     : num  7.67 5.14 7.67 6.93 7.62 7.67 5.66 7.14 6.7 7.73 ...
##  $ population    : num  9343772 29726803 13056499 24399948 20531160 ...
##  $ continent     : chr  "Asia" "Asia" "Asia" "Asia" ...

Calculate summary statistics

Check the average and median life expectancy every 10 years. In 1900 the average life expectancy was only 33 years. The standard deviation was only 6.5 years so there was very little deviation between countries. In 2010 the average life expectancy had gone up to 70 years.

allData %>% 
  group_by(year) %>%
  filter(year %% 10 == 0) %>%
  summarize(average_lifeExpectancy = mean(lifeExpectancy),
            median_lifeExpectancy = median(lifeExpectancy),
            sd_lifeExpectancy = sd(lifeExpectancy)) 

Check the average life expectancy in 1900 by continent.

allData %>% 
  group_by(continent) %>%
  filter(year == 1900) %>%
  summarize(average_lifeExpectancy = mean(lifeExpectancy),
            median_lifeExpectancy = median(lifeExpectancy),
            sd_lifeExpectancy = sd(lifeExpectancy)) %>%
            arrange(desc(average_lifeExpectancy))
## # A tibble: 5 x 4
##   continent average_lifeExpectancy median_lifeExpectancy sd_lifeExpectancy
##   <chr>                      <dbl>                 <dbl>             <dbl>
## 1 Oceania                     48.7                  48.7              1.76
## 2 Europe                      41.6                  40.4              5.88
## 3 Americas                    33.7                  32.9              5.59
## 4 Africa                      30.3                  30.7              2.42
## 5 Asia                        30.2                  31.2              4.23
allData %>% 
  group_by(continent) %>%
  filter(year == 2010) %>%
  summarize(average_lifeExpectancy = mean(lifeExpectancy),
            median_lifeExpectancy = median(lifeExpectancy),
            sd_lifeExpectancy = sd(lifeExpectancy)) %>%
            arrange(desc(average_lifeExpectancy))
## # A tibble: 5 x 4
##   continent average_lifeExpectancy median_lifeExpectancy sd_lifeExpectancy
##   <chr>                      <dbl>                 <dbl>             <dbl>
## 1 Oceania                     81.4                  81.4             0.849
## 2 Europe                      78.9                  80.0             2.67 
## 3 Americas                    74.0                  75.4             9.09 
## 4 Asia                        73.3                  74.4             6.66 
## 5 Africa                      61.0                  60.4             7.53

In 1900 the average life expectancy is highest in Oceania at 48 years and lowest in Asia at 30 years. In 2010 Oceania still has the highest life expectancy at 81 years.

Create Visualisations

The data is only available yearly from 1950 so filter out the data from before that.

allData <- allData %>%
              filter(!year < 1950)

Install ggplot2 to create visualizations.

if(!require("ggplot2")) install.packages("ggplot2")

library(ggplot2)

# Create the plot with Year as the x-axis and Life Expectancy as the y-axis, limiting y axis from 0 years to 100
ggplot(allData, aes(year, lifeExpectancy, size = population, color = continent)) +
  geom_line()+ ylim(0,100) +
 # xlim(1950,2017) +
  scale_color_brewer(type = 'div', palette = 'Spectral') +
  scale_size_continuous(labels = scales::comma) +
  labs(title = "Life Expectancy by Year", x = "Year", y = "Life Expectancy") +
 facet_wrap(continent ~., nrow = 5)

Install gganimate to create animations.

if(!require("gganimate")) devtools::install_github('thomasp85/gganimate')
library(gganimate)

# Create the plot with years as frame
ggplot(allData, aes(fertility, lifeExpectancy, size = population, color = continent, frame = year)) +
  geom_point()+ ylim(0,100)  + 
  scale_color_brewer(type = 'div', palette = 'Spectral') +
  scale_size_continuous(labels = scales::comma) +
 facet_wrap(~continent) +
 labs(title = 'Year: {frame_time}', x = 'fertility', y = 'Life expectancy', color = "continent") +
transition_time(year) +
  ease_aes('linear')