-
Notifications
You must be signed in to change notification settings - Fork 1
/
Hans Rosling’s Gapminder.R
88 lines (81 loc) · 3.17 KB
/
Hans Rosling’s Gapminder.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# PACKAGES NEEDED ####
list.of.packages <- c('WDI', 'dplyr', 'ggplot2', 'ggthemes', 'knitr', 'kableExtra', 'rnaturalearth', 'tidyverse', 'ggrepel')
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)
lapply(list.of.packages, library, character.only = T, quietly = T)
# CODE HRG 1 ####
indicators <- c(life_exp = "SP.DYN.LE00.IN",
gdp_capita ="NY.GDP.PCAP.CD",
pop = "SP.POP.TOTL")
hrg <- WDI(indicators, country="all", start = "2018", end = "2018")
Data_info <- WDI_data
Data_countries <- as.data.frame(Data_info$country)
hrg %>%
left_join(Data_countries, "iso2c") %>%
filter(region != "Aggregates") %>% # remove aggregates (groups of countries)
ggplot() +
geom_point(aes(x = gdp_capita, y = life_exp, size = pop, color = region)) +
scale_x_continuous(
labels = scales::dollar_format(),
breaks = scales::log_breaks(n = 10)) +
coord_trans(x = 'log10') +
scale_size_continuous(
labels = scales::number_format(scale = 1/1e6, suffix = "m"),
breaks = seq(1e8,1e9, 2e8),
range = c(1,20)) +
theme_minimal() +
labs(title = "An Example of Hans Rosling's Gapminder using WDI (Data for 2018)",
x = "GDP per capita (log scale)",
y = "Life expectancy at birth",
size = "Population",
color = NULL,
caption = "Source: World Bank")
# CODE HRG 2 ####
hrg2 <- hrg %>%
left_join(Data_countries, "iso2c") %>%
filter(region != "Aggregates") # remove aggregates (groups of countries)
ggplot(hrg2) +
geom_point(
aes(x = gdp_capita, y = life_exp, size = pop, color = region)) +
scale_x_continuous(
labels = scales::dollar_format(),
breaks = scales::log_breaks(n = 10)) +
coord_trans(x = 'log10') +
scale_size_continuous(
labels = scales::number_format(scale = 1/1e6, suffix = "m"),
breaks = seq(1e8,1e9, 2e8),
range = c(1,20)) +
theme_minimal() +
labs(x = "GDP per capita (log scale)",
y = "Life expectancy at birth",
size = "Population",
color = NULL,
caption = "Source: World Bank") +
geom_label_repel(data = subset(hrg2, life_exp > 84 | life_exp < 55),
aes(x = gdp_capita, y = life_exp, label = country.x),
box.padding = 0.35,
point.padding = 0.5,
segment.color = 'grey50')
# CODE HRG 3 ####
ggplot(hrg2) +
geom_point(
aes(x = gdp_capita, y = life_exp, size = pop, color = region)) +
scale_x_continuous(
labels = scales::dollar_format(),
breaks = scales::log_breaks(n = 10)) +
coord_trans(x = 'log10') +
scale_size_continuous(
labels = scales::number_format(scale = 1/1e6, suffix = "m"),
breaks = seq(1e8,1e9, 2e8),
range = c(1,20)) +
theme_minimal() +
labs(x = "GDP per capita (log scale)",
y = "Life expectancy at birth",
size = "Population",
color = NULL,
caption = "Source: World Bank") +
geom_label_repel(data = subset(hrg2, pop > 90000000), # 90 millions
aes(x = gdp_capita, y = life_exp, label = country.x),
box.padding = 0.9,
point.padding = 0.9,
segment.color = 'grey50')