Data story

Life expectancy, gdp per capita and population size

This document gives a few suggestions to analyse a dataset composed by three numeric variables. It considers an abstract of the Gapminder dataset made famous through the Hans Rosling Ted Talk. It provides the average life expectancy, gdp per capita and population size for more than 100 countries. This dataset is available through the gapminder R package and you can download it here. The table beside shows a glimpse of it.

Show code
# Libraries
library(tidyverse)
library(hrbrthemes)
library(viridis)
library(kableExtra)
options(knitr.table.format = "html")
library(plotly)
library(gridExtra)
library(ggrepel)

# The dataset is provided in the gapminder library
library(gapminder)
data <- gapminder %>% filter(year=="2007") %>% select(-year)

# show data
data %>% head(6) %>%
  mutate(gdpPercap=round(gdpPercap,0)) %>%
  mutate(pop=round(pop/1000000,2)) %>%
  mutate(lifeExp=round(lifeExp,1)) %>%
  kable() %>%
  kable_styling(bootstrap_options = "striped", full_width = F)
countrycontinentlifeExppopgdpPercap
AfghanistanAsia43.831.89975
AlbaniaEurope76.43.605937
AlgeriaAfrica72.333.336223
AngolaAfrica42.712.424797
ArgentinaAmericas75.340.3012779
AustraliaOceania81.220.4334435

Bubble plot

The go to graph in that kind of situation is probably the bubble plot. The bubble plot is really close to a scatterplot since it shows the value of two numeric variables on the X and Y axis. However, it allows to study the value of a third variable using different sizes for the dots (that are thus called bubbles).

Show code
# Show a bubbleplot
data %>%
  mutate(pop=pop/1000000) %>%
  arrange(desc(pop)) %>%
  mutate(country = factor(country, country)) %>%
  ggplot( aes(x=gdpPercap, y=lifeExp, size = pop, color = continent)) +
    geom_point(alpha=0.7) +
    scale_size(range = c(1.4, 19), name="Population (M)") +
    scale_color_viridis(discrete=TRUE, guide=FALSE) +
    theme_ipsum() +
    theme(legend.position="bottom")
Life expectancy, gdp per capita and population size – chart example

The problem with bubble plot is that the relationship between the variable of the X and Y axis is much more obvious than the relationship with the third variable. Thus you must prioritize your variables and be sure of what you want to show. Before doing that kind of chart, I believe it is a good practice to try other combinations:

Show code
p2 <- data %>%
  mutate(pop=pop/1000000) %>%
  arrange(desc(pop)) %>%
  mutate(country = factor(country, country)) %>%
  ggplot( aes(x=gdpPercap, y=pop, size = lifeExp, color = continent)) +
    geom_point(alpha=0.7) +
    scale_color_viridis(discrete=TRUE) +
    scale_y_log10() +
    theme_ipsum() +
    theme(legend.position="none")

p3 <- data %>%
  mutate(pop=pop/1000000) %>%
  arrange(desc(pop)) %>%
  mutate(country = factor(country, country)) %>%
  ggplot( aes(x=lifeExp, y=pop, size = gdpPercap, color = continent)) +
    geom_point(alpha=0.7) +
    scale_color_viridis(discrete=TRUE) +
    scale_y_log10() +
    theme_ipsum() +
    theme(legend.position="none")


grid.arrange(p2,p3, ncol=2)
Life expectancy, gdp per capita and population size – chart example

In this case there is no obvious relationship between opulation and other metrics so it makes sense to use population for the bubble size.

If you just want to highlight the relationship between gbp per capita and life Expectancy you’ve probably done most of the work now. However, it is a good practice to highlight a few interesting dots in this chart to give more insight to the plot:

Show code
# Prepare data
tmp <- data %>%
 mutate(
   annotation = case_when(
    gdpPercap > 5000 & lifeExp < 60 ~ "yes",
    lifeExp < 30 ~ "yes",
     gdpPercap > 40000 ~ "yes"
    )
) %>%
mutate(pop=pop/1000000) %>%
  arrange(desc(pop)) %>%
  mutate(country = factor(country, country))

# Plot
ggplot( tmp, aes(x=gdpPercap, y=lifeExp, size = pop, color = continent)) +
    geom_point(alpha=0.7) +
    scale_size(range = c(1.4, 19), name="Population (M)") +
    scale_color_viridis(discrete=TRUE) +
    theme_ipsum() +
    theme(legend.position="none") +
    geom_text_repel(data=tmp %>% filter(annotation=="yes"), aes(label=country), size=4 )
Life expectancy, gdp per capita and population size – chart example

Last but not least, note that bubble plot is probably the type of chart where using interactivity makes the more sense. In the following plot you can hover bubbles to get more information and zoom on a specific part of the graphic.

Show code
# Interactive version
p <- data %>%
  mutate(gdpPercap=round(gdpPercap,0)) %>%
  mutate(pop=round(pop/1000000,2)) %>%
  mutate(lifeExp=round(lifeExp,1)) %>%
  arrange(desc(pop)) %>%
  mutate(country = factor(country, country)) %>%
  mutate(text = paste("Country: ", country, "\nPopulation (M): ", pop, "\nLife Expectancy: ", lifeExp, "\nGdp per capita: ", gdpPercap, sep="")) %>%
  ggplot( aes(x=gdpPercap, y=lifeExp, size = pop, color = continent, text=text)) +
    geom_point(alpha=0.7) +
    scale_size(range = c(1.4, 19), name="Population (M)") +
    scale_color_viridis(discrete=TRUE, guide=FALSE) +
    theme_ipsum() +
    theme(legend.position="none")

ggplotly(p, tooltip="text")

A note on the grid scenario

A specific use case where three numeric columns are displayed is the grid system. In this case, the two first columns gives the grid coordinates, and the third variable gives a numeric value for each position of the grid. For example, the volcano data set provides that altitude of each point of the grid of a volcano:

Show code
# prepare the dataset:
don <- volcano
colnames(don) <- seq(1,ncol(don))
don <- don %>%
  as.tibble() %>%
  mutate(lat=seq(1,nrow(don)) ) %>%
  gather(key="long", value="altitude", -lat)

# show data
don %>% head(6) %>% kable() %>%
  kable_styling(bootstrap_options = "striped", full_width = F)
latlongaltitude
11100
21101
31102
41103
51104
61105

This kind of data can be represented using a heatmap (2d):

Show code
don %>%
  na.omit() %>%
  ggplot(aes(x=as.numeric(long), y=lat, fill=altitude)) +
    geom_tile() +
    scale_fill_viridis() +
    theme_ipsum() +
    xlab("longitude") +
    ylab("latitude")
Life expectancy, gdp per capita and population size – chart example

Another way is to build a surface plot. It really makes sense to use 3D in this special case since it allows to visualize the real shape of the volcano:

Show code
plot_ly(z = volcano, type = "surface")

Going further

You can learn more about each type of graphic presented in this story in the dedicated sections. Click the icon below:

Dataviz decision tree

Data To Viz is a comprehensive classification of chart types organized by data input format. Get a high-resolution version of our decision tree delivered to your inbox now!

High resolution poster of the Data to Viz decision tree