-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmapping-human-capital-index.R
More file actions
105 lines (88 loc) · 3.1 KB
/
mapping-human-capital-index.R
File metadata and controls
105 lines (88 loc) · 3.1 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# mapping the World Economic Forum's Human Capital Index
# human capital: cutting-edge knowledge, judgement (esp. in complex domains), & creativity
library(dplyr)
library(stringr)
library(readr)
library(rvest)
library(viridis)
library(ggmap)
##########
# GET DATA
##########
html.human_capital <- read_html("http://reports.weforum.org/human-capital-report-2016/rankings/")
df.human_capital <- html.human_capital %>%
html_node("table") %>%
html_table()
# inspect
head(df.human_capital)
# rename variables
df.human_capital <- df.human_capital %>%
rename(rank = `Overall Rank`,
country = Economy,
human_capital_score = `Overall Score`)
# inspect
head(df.human_capital)
###############
# GET WORLD MAP
###############
map.world <- map_data("world")
# make sure country names match in Human Capital DF and in the world map
human_capital_countries <- df.human_capital %>%
select(country)
# inspect
human_capital_countries
# get world map countries
world_map_countries <- map.world %>%
distinct(region)
# inspect
world_map_countries
# check match
df.match_check <- left_join(human_capital_countries, world_map_countries,
by = c('country' = 'region'))
# inspect
df.match_check
# get unmatched countries
unmatched <- df.match_check %>%
filter(is.na(match))
# inspect
unmatched
# recode unmatched countries
df.human_capital$country <- recode(df.human_capital$country,
"United Kingdom" = "UK",
"United States" = "USA",
"Russian Federation" = "Russia",
"Korea, Rep." = "South Korea",
"Kyrgyz Republic" = "Kyrgyzstan",
"Slovak Republic" = "Slovakia",
"Macedonia, FYR" = "Macedonia",
"Iran, Islamic Rep." = "Iran",
"Trinidad and Tobago" = "Trinidad",
"Lao PDR" = "Laos",
"CÙte d’Ivoire" = "Ivory Coast")
# join human capital data to world map
map.world <- left_join(map.world, df.human_capital, by = c('region' = 'country'))
# inspect
str(map.world)
head(map.world)
##########
# PLOT MAP
##########
# rough draft
gg <- ggplot(data = map.world,
aes(x = long, y = lat, group = group)) +
geom_polygon(aes(fill = human_capital_score))
# plot final map
gg +
scale_fill_viridis(option = "inferno", name = "Human Capital\nIndex") +
labs(title = "Human Capital Index, by country",
subtitle = "Source: World Economic Forum, 2016") +
theme(panel.background = element_rect(fill = "#3E3E3E"),
plot.background = element_rect(fill = "#3E3E3E"),
legend.background = element_blank(),
axis.title = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
panel.grid = element_blank(),
text = element_text(family = "American Typewriter", color = "#DDDDDD"),
plot.title = element_text(size = 32),
legend.position = c(.18, .375))