-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslides-adding-text-labels-to-leaflet-maps.qmd
90 lines (75 loc) · 1.52 KB
/
slides-adding-text-labels-to-leaflet-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
---
title: Adding Text Labels to {leaflet} Maps
---
```{r}
library(leaflet)
library(tidyverse)
library(tidycensus)
library(janitor)
library(tigris)
library(scales)
library(sf)
```
```{r}
speak_language_other_than_english_wgs84 <-
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) |>
st_transform(crs = 4326)
```
### Text
#### Labels
```{r}
leaflet() |>
addProviderTiles("CartoDB.Positron") |>
addPolygons(
data = speak_language_other_than_english_wgs84,
weight = 1
)
```
```{r}
leaflet() |>
addProviderTiles("CartoDB.Positron") |>
addPolygons(
data = speak_language_other_than_english_wgs84,
weight = 1,
label = ~name
)
```
#### Popups
```{r}
leaflet() |>
addProviderTiles("CartoDB.Positron") |>
addPolygons(
data = speak_language_other_than_english_wgs84,
weight = 1,
popup = ~name
)
```
### Create Better Labels
```{r}
speak_language_other_than_english_wgs84_with_labels <-
speak_language_other_than_english_wgs84 |>
mutate(
text_label = str_glue(
"<b>{percent(pct, accuracy = 0.1)}</b> of the population of <b>{name}</b> speaks a language other than English"
)
)
```
---
```{r}
leaflet() |>
addProviderTiles("CartoDB.Positron") |>
addPolygons(
data = speak_language_other_than_english_wgs84_with_labels,
weight = 1,
popup = ~text_label
)
```
### Your Turn {.your-turn}