-
Notifications
You must be signed in to change notification settings - Fork 0
/
visualisation.R
212 lines (168 loc) · 7.69 KB
/
visualisation.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
library(tidyverse)
library(dslabs)
data("gapminder")
#### Time series plots ####
# scatterplot of fertility rate in US by year
gapminder %>% filter(country == "United States") %>%
ggplot(aes(year, fertility)) +
geom_point()
# line plot of US fertility by year
gapminder %>% filter(country == "United States") %>%
ggplot(aes(year, fertility)) +
geom_line()
### multiple time series
# 2 line for 2 countries with group
gapminder %>% filter(country %in% c("Germany","Vietnam")) %>%
ggplot(aes(year, fertility, group = country)) +
geom_line()
# 2 line with different colors for 2 countries with color (col)
gapminder %>% filter(country %in% c("Germany","Vietnam")) %>%
ggplot(aes(year, fertility, col = country)) +
geom_line()
# use label instead of legend
labels <- data.frame(country = c("Vietnam", "Germany"),
x = c(1968, 1980), y = c(6, 2)) # create label position
gapminder %>% filter(country %in% c("Germany","Vietnam")) %>%
ggplot(aes(year, fertility, col = country)) +
geom_line() +
geom_label(data = labels, aes(x, y, label = country), size = 5) + # add label
theme(legend.position = "none") # remove legend
### Transformations ####
# add dollars per days
g <- gapminder %>% mutate(dollars_per_day = gdp/population/365)
past_year <- 1970
present_year <- 2010
# basic histogram
g %>% filter(year == past_year & !is.na(gdp)) %>%
ggplot(aes(dollars_per_day)) +
geom_histogram(binwidth = 1, col = "black")
# using log scaled data
g %>% filter(year == past_year & !is.na(gdp)) %>%
ggplot(aes(log2(dollars_per_day))) +
geom_histogram(binwidth = 1, col = "black")
gapminder %>% filter(year == present_year) %>%
ggplot(aes(log10(population))) +
geom_histogram(binwidth = 0.5, col = "black")
# using log scale
g %>% filter(year == past_year & !is.na(gdp)) %>%
ggplot(aes(dollars_per_day)) +
scale_x_continuous(trans = "log2") +
geom_histogram(binwidth = 1, col = "black")
#### Stratifying and boxplots ####
p1 <- g %>% filter(year == past_year, !is.na(gdp)) %>%
mutate(region = reorder(region, dollars_per_day, FUN = median)) %>% # change order of factor by a function apply to a numeric vector
ggplot(aes(region, dollars_per_day, fill = continent)) +
theme(axis.text.x = element_text(hjust = 1, angle = 90)) + # rotate the x-axis values
xlab("") # remove x-axis labels
p1 + geom_boxplot()
p1 + scale_y_continuous(trans = "log2") +
geom_boxplot()
p1 + scale_y_continuous(trans = "log2") +
geom_boxplot() +
geom_point(show.legend = FALSE)
#### Comparing distributions ####
west <- c("Western Europe", "Northern Europe", "Southern Europe", "Northern America", "Australia and New Zealand")
country_past <- gapminder %>%
filter(year == past_year, !is.na(gdp)) %>%
.$country
country_present <- gapminder %>%
filter(year == present_year, !is.na(gdp)) %>%
.$country
country_list <- intersect(country_past, country_present)
# comparing with histogram
g %>% filter(year == past_year & !is.na(gdp)) %>%
mutate(group = if_else(region %in% west, "West", "Developing")) %>%
ggplot(aes(dollars_per_day)) +
geom_histogram(binwidth = 1, col = "black") +
scale_x_continuous(trans = "log2") +
facet_grid(. ~ group)
g %>% filter(year == present_year & !is.na(gdp)) %>%
mutate(group = if_else(region %in% west, "West", "Developing")) %>%
ggplot(aes(dollars_per_day)) +
geom_histogram(binwidth = 1, col = "black") +
scale_x_continuous(trans = "log2") +
facet_grid(. ~ group)
g %>% filter(year %in% c(past_year, present_year),
country %in% country_list) %>% # filter countries with available data in all periods
mutate(region = reorder(region, dollars_per_day, FUN = median),
group = if_else(region %in% west, "West", "Developing")) %>%
ggplot(aes(dollars_per_day)) +
geom_histogram(binwidth = 1, color = "black") +
scale_x_continuous(trans = "log2") +
facet_grid(year~group)
# comparing with boxplots
p <- g %>% filter(year %in% c(past_year, present_year), country %in% country_list) %>%
mutate(region = reorder(region, dollars_per_day, FUN = median)) %>% # change order of factor by a function apply to a numeric vector
ggplot() +
theme(axis.text.x = element_text(hjust = 1, angle = 90)) + # rotate the x-axis values
xlab("") + # remove x-axis labels
scale_y_continuous(trans = "log2")
p + geom_boxplot(aes(region, dollars_per_day, fill = continent)) +
facet_grid(year~.)
p + geom_boxplot(aes(region, dollars_per_day, fill = factor(year)))
#### Density plots ####
# basic density plots comparing 2 distributions
g %>% filter(year %in% c(past_year, present_year),
country %in% country_list) %>%
ggplot(aes(dollars_per_day)) +
geom_density(fill = "grey") +
scale_x_continuous(trans = "log2") +
facet_grid(. ~ year)
# change the y-axis to count (computed value by density)
g %>% filter(year %in% c(past_year, present_year),
country %in% country_list) %>%
mutate(group = if_else(region %in% west, "West", "Developing")) %>%
ggplot() +
geom_density(aes(x = dollars_per_day, y = ..count.., fill = group),
alpha = 0.2, bw = 0.75) +
scale_x_continuous(trans = "log2") +
facet_grid(.~year)
# add new region groups with case_when
g1 <- g %>% mutate(group = case_when(
.$region %in% west ~ "West",
.$region %in% c("Eastern Asia", "South-Eastern Asia") ~ "East Asia",
.$region %in% c("Caribbean", "Central America", "South America") ~ "Latin America",
.$continent == "Africa" & .$region != "Northern Africa" ~ "Sub-Saharan Africa",
TRUE ~ "Others"
)) %>% mutate(group = factor(group, levels = c("Others", "Latin America", "East Asia",
"Sub-Saharan Africa", "West")))
# stacked density plots
g1 %>% filter(year %in% c(past_year, present_year),
country %in% country_list) %>%
ggplot(aes(dollars_per_day, fill = group)) +
scale_x_continuous(trans = "log2") +
geom_density(aes(y = ..count..), alpha = 0.2, bw = 0.75, position = "stack") +
facet_grid(year ~ .)
# add weight by population ??
g1 %>% filter(year %in% c(past_year, present_year),
country %in% country_list) %>%
group_by(year) %>%
mutate(weight = population/sum(population*2)) %>%
ungroup() %>%
ggplot(aes(dollars_per_day, fill = group, weight = weight)) +
scale_x_continuous(trans = "log2") +
geom_density(alpha = 0.2, bw = 0.75, position = "stack") +
facet_grid(year ~ .)
#### Logistic (logit) transformation ####
g2 <- g %>%
mutate(group = case_when(
.$region %in% west ~ "West",
.$region %in% c("Eastern Asia", "South-Eastern Asia") ~ "East Asia",
.$region %in% c("Central America", "South America", "Caribbean") ~ "Latin America",
.$region %in% c("Melanesia", "Micronesia", "Polynesia") ~ "Pacific Islands",
.$continent == "Africa" & .$region != "Northern Africa" ~ "Sub-Saharan Africa",
TRUE ~ as.character(.$region)
))
# make new data frame with group average income and avera infant survival rate
surv_income <- g2 %>%
filter(year == present_year, !is.na(gdp), !is.na(infant_mortality), !is.na(group)) %>%
group_by(group) %>%
summarise(income = sum(gdp)/sum(population)/365,
infant_survival_rate = 1 - sum(infant_mortality/1000*population)/sum(population)) %>%
arrange(income)
# plot infant survival rate versus income
surv_income %>% ggplot(aes(income, infant_survival_rate, label = group, col = group)) +
scale_x_continuous(trans = "log2", limits = c(.25, 150)) +
scale_y_continuous(trans = "logit", limits = c(.875, .9981),
breaks = c(.85, .90, .95, .99, .995, .998)) +
geom_label(size = 3, show.legend = FALSE)