The aim of this interactive visualisation is to shed light on the Life Expectancy across the globe and explore its relationship with GDP per Capita.
The data set is obtained from the ‘gapminder’ package prepared by Gapminder.
| Type | Challenge | Solution |
|---|---|---|
| Data | Sourcing for relevant data sets and integrating them together poses a challenge | Found the ‘gapminder’ package which provides all the data required |
| Data | Getting the necessary columns to plot a choropleth map | Use ‘gapminder’ package to retrieve more columns |
| Design | Finding a suitable plot type that involves more variables for better insights | Use a bubble chart along with time slider |
| Design | No obvious distinction between the countries on choropleth map as colours are too similar for most countries | Use diverging colour palette for choropleth map |
packages = c('plotly','gapminder','tidyverse')
for(p in packages){
if(!require(p, character.only = T)){
install.packages(p)
}
library(p, character.only = T)
}
country_data <- gapminder
country_data
## # A tibble: 1,704 x 6
## country continent year lifeExp pop gdpPercap
## <fct> <fct> <int> <dbl> <int> <dbl>
## 1 Afghanistan Asia 1952 28.8 8425333 779.
## 2 Afghanistan Asia 1957 30.3 9240934 821.
## 3 Afghanistan Asia 1962 32.0 10267083 853.
## 4 Afghanistan Asia 1967 34.0 11537966 836.
## 5 Afghanistan Asia 1972 36.1 13079460 740.
## 6 Afghanistan Asia 1977 38.4 14880372 786.
## 7 Afghanistan Asia 1982 39.9 12881816 978.
## 8 Afghanistan Asia 1987 40.8 13867957 852.
## 9 Afghanistan Asia 1992 41.7 16317921 649.
## 10 Afghanistan Asia 1997 41.8 22227415 635.
## # … with 1,694 more rows
bubble <- country_data %>%
plot_ly(
x = ~gdpPercap,
y = ~lifeExp,
color = ~continent,
size = ~pop,
frame = ~year,
text = ~country,
hoverinfo = ~text,
type = 'scatter',
mode = 'markers'
) %>% layout(
title = "Life Expectancy against GDP per Capita by Continent/Country, 1952-2007",
xaxis = list(
title = "GDP per Capita",
type = "log"
),
yaxis = list(
title = "Life Expectancy",
range = c(0,90)
)
) %>% animation_slider(
currentvalue = list(
prefix = "Year: ",
font = list(color="skyblue")
)
)
bubble
gapminder_codes <- gapminder::country_codes
gapminder_unfiltered <-gapminder::gapminder_unfiltered
# We join both datasets with inner_join to get a dataset with the info by country, continent and country-code
gapminder_join <- gapminder_unfiltered %>%
inner_join(gapminder_codes, by= "country") %>%
mutate(code = iso_alpha)
gapminder_data <- gapminder_join %>%
inner_join(maps::iso3166 %>%
select(a3, mapname), by= c(code = "a3")) %>%
mutate
gapminder_data
## # A tibble: 3,584 x 10
## country continent year lifeExp pop gdpPercap iso_alpha iso_num code
## <chr> <fct> <int> <dbl> <int> <dbl> <chr> <int> <chr>
## 1 Afghanistan Asia 1952 28.8 8.43e6 779. AFG 4 AFG
## 2 Afghanistan Asia 1957 30.3 9.24e6 821. AFG 4 AFG
## 3 Afghanistan Asia 1962 32.0 1.03e7 853. AFG 4 AFG
## 4 Afghanistan Asia 1967 34.0 1.15e7 836. AFG 4 AFG
## 5 Afghanistan Asia 1972 36.1 1.31e7 740. AFG 4 AFG
## 6 Afghanistan Asia 1977 38.4 1.49e7 786. AFG 4 AFG
## 7 Afghanistan Asia 1982 39.9 1.29e7 978. AFG 4 AFG
## 8 Afghanistan Asia 1987 40.8 1.39e7 852. AFG 4 AFG
## 9 Afghanistan Asia 1992 41.7 1.63e7 649. AFG 4 AFG
## 10 Afghanistan Asia 1997 41.8 2.22e7 635. AFG 4 AFG
## # … with 3,574 more rows, and 1 more variable: mapname <chr>
world_map <- map_data("world") %>%
filter(region != "Antartica")
gapminder_life_exp <- gapminder_data %>%
filter(year == 2007) %>%
select(mapname, code, lifeExp)
map <- plot_geo(gapminder_life_exp) %>%
add_trace(
z = ~lifeExp,
color = ~lifeExp,
colors = 'RdBu',
text = ~mapname,
locations = ~code
) %>%
colorbar(title = "Years") %>%
layout(title = 'Life Expectancy around the World, 2007',
geo = gapminder_life_exp
)
map
bubble
map
The top visualization is a bubble chart which reveals the life expectancy against GDP per capita ratio by each continent and country (tooltip). Additional information like the population size of the country is reflected in the size of the bubble as well. An interactive timeline slider is also provided for readers to toggle and observe the shift across the years.
The bottom visualization focuses on life expectancy of countries across the globe, highlighting the continents with high/low life expectancy. Through the comparison with the map of the continents, readers can get a good sensing of the general life expectancy of the continents as well.