forked from EDJNet/internet_speed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
4-density_plots.R
164 lines (130 loc) · 7.86 KB
/
4-density_plots.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
## Libraries
if (!require("pacman")) install.packages("pacman")
pacman::p_load(tidyverse,
here,
cowplot,
ggtext)
dir.create(here("visualizations", "density_plots"))
## Density plots regions
region <- lau_matched %>%
pull(region) %>%
unique()
for (i in region) {
region_i <- lau_matched %>% filter(region == i)
country_i <- lau_matched %>% filter(country_id == unique(region_i$country_id))
if(nrow(region_i) == 1) {
plt <- ggplot() +
geom_segment(aes(x = 64, y = 0, xend = 64, yend = Inf), size = 1, linetype = 2) +
geom_density(data = lau_matched, aes(avg_d), color = "#283377", fill = "#283377", alpha = .2, size = 1) +
geom_density(data = country_i, aes(avg_d), color = "#337728", fill = "#337728", alpha = .2, size = 1) +
geom_segment(aes(x = region_i$avg_d, y = 0, xend = region_i$avg_d, yend = Inf), size = 1, linetype = 1, color = "#a9073b") +
scale_x_continuous(limits = c(0, 305), expand = c(0,0), breaks = c(0, 30, 50, 64, 100, 150, 200, 250, 300)) +
scale_y_continuous(expand = c(0, 0)) +
xlab("Download speed (Mbps)") +
ylab("") +
labs(title = "Municipalities' average download speed distribution <br> in <span style = 'color:#283377'>Europe</span>, <span style = 'color:#337728'>" %>%
paste0(unique(country_i$Country)) %>%
paste0("</span>, and <span style = 'color:#a9073b'>") %>%
paste0(unique(region_i$region)) %>%
paste0("</span>"),
subtitle = "64 Mbps is the median download speed of European cities") +
theme_half_open() +
theme(plot.title = element_markdown(hjust = .5),
plot.subtitle = element_text(hjust = .5),
axis.line = element_line(colour = "black", size = 1),
axis.ticks = element_line(colour = "black", size = 1))
ggsave(filename = here("visualizations", "density_plots", i %>% paste0(".png")), plt, width = 23, height = 13, units = "cm", dpi = 333)
} else {
if(max(region_i$avg_d) > 250) {
plt <- ggplot() +
geom_segment(aes(x = 64, y = 0, xend = 64, yend = Inf), size = 1, linetype = 2) +
geom_density(data = lau_matched, aes(avg_d), color = "#283377", fill = "#283377", alpha = .2, size = 1) +
geom_density(data = country_i, aes(avg_d), color = "#337728", fill = "#337728", alpha = .2, size = 1) +
geom_density(data = region_i, aes(avg_d), color = "#a9073b", fill = "#a9073b", alpha = .2, size = 1) +
scale_x_continuous(limits = c(0, 355), expand = c(0,0), breaks = c(0, 30, 50, 64, 100, 150, 200, 250, 300, 350)) +
scale_y_continuous(expand = c(0,0)) +
xlab("Download speed (Mbps)") +
ylab("") +
labs(title = "Municipalities' average download speed distribution <br> in <span style = 'color:#283377'>Europe</span>, <span style = 'color:#337728'>" %>%
paste0(unique(country_i$Country)) %>%
paste0("</span>, and <span style = 'color:#a9073b'>") %>%
paste0(unique(region_i$region)) %>%
paste0("</span>"),
subtitle = "64 Mbps is the median download speed of European cities") +
theme_half_open() +
theme(plot.title = element_markdown(hjust = .5),
plot.subtitle = element_text(hjust = .5),
axis.line = element_line(colour = "black", size = 1),
axis.ticks = element_line(colour = "black", size = 1))
ggsave(filename = here("visualizations", "density_plots", i %>% paste0(".png")), plt, width = 23, height = 13, units = "cm", dpi = 333)
} else {
plt <- ggplot() +
geom_segment(aes(x = 64, y = 0, xend = 64, yend = Inf), size = 1, linetype = 2) +
geom_density(data = lau_matched, aes(avg_d), color = "#283377", fill = "#283377", alpha = .2, size = 1) +
geom_density(data = country_i, aes(avg_d), color = "#337728", fill = "#337728", alpha = .2, size = 1) +
geom_density(data = region_i, aes(avg_d), color = "#a9073b", fill = "#a9073b", alpha = .2, size = 1) +
scale_x_continuous(limits = c(0, 305), expand = c(0,0), breaks = c(0, 30, 50, 64, 100, 150, 200, 250, 300)) +
scale_y_continuous(expand = c(0,0)) +
xlab("Download speed (Mbps)") +
ylab("") +
labs(title = "Municipalities' average download speed distribution <br> in <span style = 'color:#283377'>Europe</span>, <span style = 'color:#337728'>" %>%
paste0(unique(country_i$Country)) %>%
paste0("</span>, and <span style = 'color:#a9073b'>") %>%
paste0(unique(region_i$region)) %>%
paste0("</span>"),
subtitle = "64 Mbps is the median download speed of European cities") +
theme_half_open() +
theme(plot.title = element_markdown(hjust = .5),
plot.subtitle = element_text(hjust = .5),
axis.line = element_line(colour = "black", size = 1),
axis.ticks = element_line(colour = "black", size = 1))
ggsave(filename = here("visualizations", "density_plots", i %>% paste0(".png")), plt, width = 23, height = 13, units = "cm", dpi = 333)
}
}
}
## Density plots countries
country <- lau_matched %>%
pull(Country) %>%
unique()
for (i in country) {
country_i <- lau_matched %>% filter(Country == i)
if(max(country_i$avg_d) > 250) {
plt <- ggplot() +
geom_segment(aes(x = 64, y = 0, xend = 64, yend = Inf), size = 1, linetype = 2) +
geom_density(data = lau_matched, aes(avg_d), color = "#283377", fill = "#283377", alpha = .2, size = 1) +
geom_density(data = country_i, aes(avg_d), color = "#337728", fill = "#337728", alpha = .2, size = 1) +
scale_x_continuous(limits = c(0, 355), expand = c(0,0), breaks = c(0, 30, 50, 64, 100, 150, 200, 250, 300, 350)) +
scale_y_continuous(expand = c(0,0)) +
xlab("Download speed (Mbps)") +
ylab("") +
labs(title = "Municipalities' average download speed distribution <br> in <span style = 'color:#283377'>Europe</span>, and <span style = 'color:#337728'>" %>%
paste0(unique(country_i$Country)) %>%
paste0("</span>"),
subtitle = "64 Mbps is the median download speed of European cities") +
theme_half_open() +
theme(plot.title = element_markdown(hjust = .5),
plot.subtitle = element_text(hjust = .5),
axis.line = element_line(colour = "black", size = 1),
axis.ticks = element_line(colour = "black", size = 1))
ggsave(filename = here("visualizations", "density_plots", i %>% paste0(".png")), plt, width = 23, height = 13, units = "cm", dpi = 333)
} else {
plt <- ggplot() +
geom_segment(aes(x = 64, y = 0, xend = 64, yend = Inf), size = 1, linetype = 2) +
geom_density(data = lau_matched, aes(avg_d), color = "#283377", fill = "#283377", alpha = .2, size = 1) +
geom_density(data = country_i, aes(avg_d), color = "#337728", fill = "#337728", alpha = .2, size = 1) +
scale_x_continuous(limits = c(0, 305), expand = c(0,0), breaks = c(0, 30, 50, 64, 100, 150, 200, 250, 300)) +
scale_y_continuous(expand = c(0,0)) +
xlab("Download speed (Mbps)") +
ylab("") +
labs(title = "Municipalities' average download speed distribution <br> in <span style = 'color:#283377'>Europe</span>, and <span style = 'color:#337728'>" %>%
paste0(unique(country_i$Country)) %>%
paste0("</span>"),
subtitle = "64 Mbps is the median download speed of European cities") +
theme_half_open() +
theme(plot.title = element_markdown(hjust = .5),
plot.subtitle = element_text(hjust = .5),
axis.line = element_line(colour = "black", size = 1),
axis.ticks = element_line(colour = "black", size = 1))
ggsave(filename = here("visualizations", "density_plots", i %>% paste0(".png")), plt, width = 23, height = 13, units = "cm", dpi = 333)
}
}