-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01-points.R
151 lines (115 loc) · 5.25 KB
/
01-points.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
# description ------------------------------------------------------------
# 2024-11-01
# "30DayMapChallenge classic: A map with points. Start the challenge with points. Show individual locations—anything from cities to trees or more abstract concepts. Simple, but a key part of the challenge."
# setup packages ---------------------------------------------------------
if (!require("pak")) install.packages("pak")
options(repos = c(rOpenSpain = "https://ropenspain.r-universe.dev", CRAN = "https://cloud.r-project.org"))
packages <- c("sf", "tidyverse", "spanishoddata", "mapSpain", "ggimage", "rostemplate", "ggtext", "resmush", "svglite")
pak::pkg_install(packages, upgrade = TRUE, ask = FALSE)
suppressPackageStartupMessages(invisible(lapply(packages, library, character.only = TRUE)))
rm(packages)
# get the flows data -----------------------------------------------------
od <- spod_get("od", zones = "distr", dates = "2024-04-17")
# aggregate data ---------------------------------------------------------
incoming_trips_ratio <- od |>
filter(!is.na(sex)) |>
filter(activity_origin == "home" & activity_destination == "work_or_study") |>
group_by(id_destination, sex) |>
summarise(total_trips = sum(n_trips, na.rm = TRUE), .groups = "drop") |>
collect() |>
spread(sex, total_trips, fill = 0) |>
mutate(sex_ratio = male / female) |>
mutate(sex_ratio_category = if_else(
condition = sex_ratio < 1,
true = "More Female Trips",
false = "More Male Trips")
) |>
filter(id_destination != "external") |>
select(id_destination, sex_ratio, sex_ratio_category)
# get zones and add the aggregation results ------------------------------
zones_distr_v2_centroids <- spod_get_zones("distr", ver = 2) |>
filter(!grepl("FR.*|PT.*", id)) |>
st_point_on_surface() |>
st_transform(4326) |>
left_join(incoming_trips_ratio, by = c("id" = "id_destination")) |>
select(id, sex_ratio, sex_ratio_category)
# move Canary Islands closer to Spain ------------------------------------
dots_sf <- rbind(
# all zones except for Canary Islands
zones_distr_v2_centroids |>
filter(!grepl("^38|^35", id)),
# Canary Islands moved closer to mainland Spain
esp_move_can(
zones_distr_v2_centroids |> filter(grepl("^38|^35", id))
)
) |>
mutate(sex_ratio_category = if_else(is.na(sex_ratio_category), "No Data Available", sex_ratio_category))
# prepare paths to logos -------------------------------------------------
spod_logo <- system.file("help/figures", "logo.png", package = "spanishoddata")
ropenspain_logo <- system.file("help/figures", "logo.png", package = "rostemplate")
# prepare the map --------------------------------------------------------
map_01_points <- ggplot() +
geom_sf(
data = dots_sf,
aes(col = sex_ratio_category),
size = 0.5,
alpha = 0.8,
shape = 15) +
scale_color_manual(
values = c(
"More Female Trips" = "#f7de38",
"More Male Trips" = "#440852",
"No Data Available" = "grey80"),
labels = c(
"More Female Trips" = "<span style='color:#f7de38'>More Female Trips</span>",
"More Male Trips" = "<span style='color:#440852'>More Male Trips</span>",
"No Data Available" = "<span style='color:grey80'>No Data Available</span>"),
name = "<span style='color:grey10'>Commuters</span></b>"
) +
geom_image(data = tibble(x = 3, y = 37),
aes(image = spod_logo, x = x, y = y), size = 0.3) +
geom_image(data = tibble(x = 2.05, y = 35),
aes(image = ropenspain_logo, x = x, y = y), size = 0.05) +
annotate("text", x = 2.55, y = 35, label = "rOpenSpain", size = 3, hjust = 0, color = "grey30") +
# annotate("text", x = 1.55, y = 32.5, label = "Author: Egor Kotov", size = 3, hjust = 0, color = "grey30", family = "Roboto") +
labs(
title = "Dominance of <span style='color:#f7de38'>Female</span> Commuters<br>in Urban Destinations Across Spain",
caption = "Author: Egor Kotov | #30DayMapChallenge | Day 1: Points\nR package to get the data: https://ropenspain.github.io/spanishoddata/\nOriginal data source: Ministry of Transport and Sustainable Mobility of Spain; Nommon"
) +
theme_void() +
theme(
text = element_text(family = "Roboto", size = 18),
panel.background = element_rect(fill = "grey65", color = NA),
plot.background = element_rect(fill = "grey65", color = NA),
legend.text = element_markdown(),
legend.title = element_markdown(),
plot.title = element_markdown(hjust = 0.5, color = "grey10"),
plot.subtitle = element_markdown(),
plot.caption = element_text(size = 9, hjust = 0.5, color = "grey30"),
plot.margin = margin(t = 20, r = 5, b = 20, l = 5),
legend.position = "inside",
legend.position.inside = c(0.18, 0.6)
)
# print(map_01_points)
# save map to png --------------------------------------------------------
ggsave(
filename = "maps/01-points.png",
map_01_points,
width = 8,
height = 7,
units = "in",
dpi = 300,
create.dir = TRUE
)
# opmimise png with resmush.it -------------------------------------------
resmush_file("maps/01-points.png", overwrite = TRUE)
# save to svg ------------------------------------------------------------
ggsave(
filename = "maps/01-points.svg",
map_01_points,
width = 8,
height = 7,
units = "in",
dpi = 300,
create.dir = TRUE
)