Gapminder
Graphs with gapminder data
Data
# devtools::install_github("derekmichaelwright/agData")
library(agData)
library(gapminder)
library(gganimate)
<- "www.dblogr.com/ or derekmichaelwright.github.io/dblogr/ | Data: gapminder"
myCaption ::datatable(gapminder %>% arrange(year)) DT
Plots
By Year
<- ggplot(gapminder %>% filter(year %in% c(1952, 1982, 2007)),
mp aes(x = gdpPercap / 1000, y = lifeExp, size = pop, colour = country)) +
geom_point(alpha = 0.7) +
scale_colour_manual(values = country_colors) +
scale_size(range = c(2, 12)) +
scale_x_log10() +
facet_wrap(. ~ year, ncol = 5) +
theme_agData(legend.position = "none") +
labs(x = "GDP Per Capita ($1000)", y = "Life Expectancy", caption = myCaption)
ggsave("gapminder_01.png", mp, width = 6, height = 4)
Life Expectancy By Region
<- ggplot(gapminder, aes(x = year, y = lifeExp, colour = country)) +
mp geom_line(alpha = 0.7) +
scale_colour_manual(values = country_colors) +
scale_x_continuous(breaks = seq(1950, 2010, 20)) +
coord_cartesian(xlim = c(1950, 2010)) +
facet_wrap(. ~ continent, ncol = 5) +
theme_agData(legend.position = "none",
axis.text.x = element_text(angle = 45, hjust = 1)) +
labs(x = NULL, y = "Life Expectancy", caption = myCaption)
ggsave("gapminder_02.png", mp, width = 10, height = 5)
GDP Per Capita By Region
<- ggplot(gapminder, aes(x = year, y = gdpPercap / 1000, colour = country)) +
mp geom_line(alpha = 0.7) +
scale_colour_manual(values = country_colors) +
scale_x_continuous(breaks = seq(1950, 2010, 20)) +
scale_y_log10() +
coord_cartesian(xlim = c(1950, 2010)) +
facet_wrap(. ~ continent, ncol = 5) +
theme_agData(legend.position = "none",
axis.text.x = element_text(angle = 45, hjust = 1)) +
labs(x = NULL, y = "GDP Per Capita ($1000)", caption = myCaption)
ggsave("gapminder_03.png", mp, width = 10, height = 5)
Animations
GDP Per Capita x Life Expectancy
# gganimate example
<- ggplot(gapminder, aes(x = gdpPercap / 1000, y = lifeExp,
mp size = pop, fill = country)) +
geom_point(alpha = 0.7, show.legend = F, pch = 21, color = "black") +
scale_fill_manual(values = country_colors) +
scale_size(range = c(2, 12)) +
scale_x_log10() +
facet_wrap(. ~ continent, ncol = 5) +
theme_agData() +
labs(title = 'Year: {frame_time}', caption = myCaption,
y = 'Life Expectancy', x = 'GDP Per Capita ($1000)') +
transition_time(year)
anim_save("gapminder_gif_01.gif", mp,
nframes = 300, fps = 20, end_pause = 60,
width = 900, height = 600, res = 150)
GDP Per Capita By Region
# Prep data
<- gapminder %>% arrange(rev(year), gdpPercap) %>%
xx mutate(country = factor(country, levels = unique(country)))
# Plot
<- ggplot(xx, aes(x = country, y = gdpPercap / 1000,
mp size = pop, fill = country)) +
geom_point(alpha = 0.7, show.legend = F, pch = 21, color = "black") +
scale_fill_manual(values = country_colors) +
scale_size(range = c(2, 12)) +
scale_y_log10() +
theme_agData(axis.text.x = element_blank(),
axis.ticks.x = element_blank()) +
labs(title = 'Year: {frame_time}', caption = myCaption,
x = NULL, y = 'GDP Per Capita ($1000)') +
transition_time(year)
anim_save("gapminder_gif_02.gif", mp,
nframes = 300, fps = 20, end_pause = 60,
width = 900, height = 600, res = 150)
Life Expectancy By Region
# Prep data
<- gapminder %>% arrange(rev(year), lifeExp) %>%
xx mutate(country = factor(country, levels = unique(country)))
# Plot
<- ggplot(xx, aes(x = country, y = lifeExp,
mp size = pop, fill = country)) +
geom_point(alpha = 0.7, show.legend = F, pch = 21, color ="black") +
scale_fill_manual(values = country_colors) +
scale_size(range = c(2, 12)) +
scale_y_log10() +
theme_agData(axis.text.x = element_blank(),
axis.ticks.x = element_blank()) +
labs(title = 'Year: {frame_time}', caption = myCaption,
x = NULL, y = 'Life Expectancy') +
transition_time(year)
anim_save("gapminder_gif_03.gif", mp,
nframes = 300, fps = 20, end_pause = 60,
width = 900, height = 600, res = 150)
<- function(x, myTitle = NULL) {
gg_gapminder1 ggplot(x, aes(x = gdpPercap / 1000, y = lifeExp,
size = pop, fill = country)) +
geom_point(alpha = 0.7, pch = 21, color = "black") +
geom_text(aes(label = countryLabel), alpha = 0.7, alpha = 0.3) +
scale_fill_manual(values = country_colors) +
scale_size(range = c(2, 12)) +
scale_x_log10() +
theme_agData(legend.position = "none") +
labs(title = myTitle, subtitle = "Year: {frame_time}", caption = myCaption,
y = "Life Expectancy", x = "GDP Per Capita ($1000)") +
transition_time(year)
}
# Prep data
<- c("China", "United States", "India")
areas <- gapminder %>%
xx mutate(countryLabel = ifelse(country %in% areas, as.character(country), NA))
# Plot
<- gg_gapminder1(xx)
mp anim_save("gapminder_gif_04.gif", mp,
nframes = 300, fps = 20, end_pause = 60,
width = 900, height = 600, res = 150)
Regions
Africa
# Prep data
<- c("South Africa", "Egypt", "Nigeria", "Ethiopia")
areas <- gapminder %>%
xx filter(continent == "Africa") %>%
mutate(countryLabel = ifelse(country %in% areas, as.character(country), NA))
# Plot
<- gg_gapminder1(xx, "Africa")
mp anim_save("gapminder_gif_05.gif", mp,
nframes = 300, fps = 20, end_pause = 60,
width = 900, height = 600, res = 150)
Americas
# Prep data
<- c("United States", "Canada", "Brazil", "Mexico")
areas <- gapminder %>%
xx filter(continent == "Americas") %>%
mutate(countryLabel = ifelse(country %in% areas, as.character(country), NA))
# Plot
<- gg_gapminder1(xx, "Americas")
mp anim_save("gapminder_gif_06.gif", mp,
nframes = 300, fps = 20, end_pause = 60,
width = 900, height = 600, res = 150)
Asia
# Prep data
<- c("China", "India", "Japan", "Bangladesh")
areas <- gapminder %>%
xx filter(continent == "Asia") %>%
mutate(countryLabel = ifelse(country %in% areas, as.character(country), NA))
# Plot
<- gg_gapminder1(xx, "Asia")
mp anim_save("gapminder_gif_07.gif", mp,
nframes = 300, fps = 20, end_pause = 60,
width = 900, height = 600, res = 150)
Europe
# Prep data
<- c("Germany", "France", "Russia", "Turkey")
areas <- gapminder %>%
xx filter(continent == "Europe") %>%
mutate(countryLabel = ifelse(country %in% areas, as.character(country), NA))
# Plot
<- gg_gapminder1(xx, "Europe")
mp anim_save("gapminder_gif_08.gif", mp,
nframes = 300, fps = 20, end_pause = 60,
width = 900, height = 600, res = 150)
Countries
<- function(mycountries) {
gg_gapminder2 <- gapminder %>% filter(country %in% mycountries)
x ggplot(x, aes(x = gdpPercap / 1000, y = lifeExp, fill = country)) +
geom_point(size = 3, pch = 21, color = "black") +
geom_line(alpha = 0.7) +
scale_colour_manual(values = country_colors) +
scale_x_log10() +
theme_agData(legend.position = "bottom") +
labs(title = "Year: {frame_along}", caption = myCaption,
y = "Life Expectancy", x = "GDP Per Capita ($1000)") +
transition_reveal(year)
}
Canada
<- gg_gapminder2("Canada")
mp anim_save("gapminder_gif_09.gif", mp,
nframes = 300, fps = 20, end_pause = 60,
width = 900, height = 600, res = 150)
China
<- gg_gapminder2("China")
mp anim_save("gapminder_gif_10.gif", mp,
nframes = 300, fps = 20, end_pause = 60,
width = 900, height = 600, res = 150)
Rwanda, South Africa, Nigeria
<- gg_gapminder2(c("Rwanda", "South Africa", "Nigeria"))
mp anim_save("gapminder_gif_11.gif", mp,
nframes = 300, fps = 20, end_pause = 60,
width = 900, height = 600, res = 150)
Heatmaps
# Prep data
<- c("darkred", "darkgoldenrod2", "steelblue")
myColors <- gapminder %>% filter(continent == "Africa")
xx <- unique(xx$year)
myYears <- xx %>% filter(year == 2007) %>%
myAreas arrange(lifeExp) %>% pull(country) %>% unique()
<- xx %>% mutate(country = factor(country, level = myAreas))
xx # Plot
<- ggplot(xx, aes(x = year, y = country, fill = lifeExp)) +
mp geom_tile(color = "white", size = 0.35) +
scale_x_continuous(expand = c(0,0), breaks = myYears) +
scale_fill_gradientn(name = "Life Expectancy", na.value = 'white',
colors = myColors) +
theme_agData(legend.position = "bottom",
bgFill = "white") +
labs(x = NULL, y = NULL, caption = myCaption)
ggsave("gapminder_04.png", mp, width = 6, height = 10)