-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslides-adding-text-to-maps.qmd
205 lines (168 loc) · 3.47 KB
/
slides-adding-text-to-maps.qmd
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
---
title: Adding Text to Maps
---
```{r}
#| output: false
#| echo: false
library(tidyverse)
library(tidycensus)
library(janitor)
library(tigris)
library(scales)
speak_language_other_than_english <-
get_acs(
geography = "county",
variable = "S1601_C01_003",
summary_var = "S1601_C01_001",
geometry = TRUE
) |>
clean_names() |>
mutate(pct = estimate / summary_est) |>
select(name, pct)
languages_map <-
speak_language_other_than_english |>
shift_geometry() |>
ggplot() +
geom_sf(
aes(
fill = pct,
color = pct
)
) +
guides(
fill = guide_colorsteps(show.limits = TRUE),
color = guide_colorsteps(show.limits = TRUE),
) +
scale_fill_viridis_c(
limits = c(0, 1),
labels = percent_format()
) +
scale_color_viridis_c(
limits = c(0, 1),
labels = percent_format()
) +
labs(
color = NULL,
fill = NULL,
title = "Percentage of People Who Speak a Language\nOther than English in Each County in the United States"
) +
theme_void() +
theme(
legend.position = "top",
legend.key.width = unit(2, "cm"),
legend.key.height = unit(0.5, "cm"),
plot.title = element_text(
hjust = 0.5,
face = "bold",
size = 16,
margin = margin(
b = 0.25,
unit = "cm"
)
)
)
```
### Add Text to Map
```{r}
#| output: false
oregon_counties <-
counties(state = "OR") |>
clean_names()
```
```{r}
oregon_counties
```
---
```{r}
oregon_counties |>
ggplot() +
geom_sf() +
geom_sf_text(aes(label = name))
```
### Map from Last Lesson
---
```{r}
languages_map
```
::: {.notes}
Show this map: https://show.rfor.us/XlwwHHkF ([source](https://links.mimestream.com/g/[email protected]/t/194b89b5675c462b))
:::
### `geom_sf_text()`
```{r}
speak_language_other_than_english
```
---
```{r}
#| output-location: slide
languages_map +
geom_sf_text(aes(label = name))
```
### Only Show Some Text
---
```{r}
library(sf)
top_counties <-
speak_language_other_than_english |>
separate_wider_delim(name, delim = ", ", names = c("county", "state")) |>
filter(state != "Puerto Rico") |>
slice_max(order_by = pct, n = 5) |>
mutate(map_label = str_glue("{county} ({percent(pct, accuracy = 1)})")) |>
st_as_sf()
```
---
```{r}
top_counties
```
---
```{r}
#| output-location: slide
languages_map +
geom_sf_text(
data = top_counties,
aes(label = map_label)
)
```
### Use {ggrepel} to Avoid Overlapping Text
---
```{r}
#| output-location: slide
#| code-line-numbers: "4,10"
library(ggrepel)
languages_map +
geom_text_repel(
data = top_counties,
aes(
label = map_label,
geometry = geometry
),
stat = "sf_coordinates"
)
```
### Additional Tweaks to Improve {ggrepel}
```{r}
#| output-location: slide
#| code-line-numbers: "9-16"
languages_map +
geom_text_repel(
data = top_counties,
aes(
label = map_label,
geometry = geometry
),
stat = "sf_coordinates",
seed = 1234,
box.padding = unit(10, "pt"),
segment.color = "gray30",
segment.curvature = -0.1,
arrow = arrow(length = unit(2, "pt"), type = "closed"),
color = "white",
bg.color = "grey30",
bg.r = 0.15
)
```
### Your Turn {.your-turn}
Improve the map you made in the last lesson on the number of refugees from each country in the world by:
1. Calculating the three top countries refugee-sending countries
1. Adding text to the map to highlight these countries
### Learn More
- Link to highlighting with shadows blog post