-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslides-mapping-with-ggplot.qmd
106 lines (82 loc) · 1.77 KB
/
slides-mapping-with-ggplot.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
---
title: Mapping with ggplot
---
```{r}
#| echo: false
options(tigris_use_cache = TRUE)
```
### Get Data
```{r}
library(tidyverse)
library(tidycensus)
library(janitor)
```
```{r}
#| code-line-numbers: "3"
load_variables(
year = 2023,
dataset = "acs5/subject",
cache = TRUE
) |>
filter(str_detect(name, "S1601"))
```
---
```{r}
#| code-line-numbers: "4,5"
get_acs(
geography = "county",
variable = "S1601_C01_003",
summary_var = "S1601_C01_001"
)
```
---
```{r}
#| output: false
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)
```
```{r}
speak_language_other_than_english
```
### Make a Map
```{r}
speak_language_other_than_english |>
ggplot() +
geom_sf()
```
### Shift Geometry
```{r}
#| code-line-numbers: "4"
library(tigris)
speak_language_other_than_english |>
shift_geometry() |>
ggplot() +
geom_sf()
```
### Aesthetic Properties
#### Fill
```{r}
speak_language_other_than_english |>
shift_geometry() |>
ggplot() +
geom_sf(aes(fill = pct))
```
#### Adjust Legend
## Your Turn {.your-turn}
- Import a [dataset I've created on the number of refugees from each country in the world](https://raw.githubusercontent.com/rfortherestofus/mapping-with-r-v2/refs/heads/main/data/refugees.geojson)
- Use ggplot to make a map of this data, with countries sending higher numbers of refugees in a different color than countries with lower numbers of refugees
::: {.notes}
```{r}
read_sf("https://raw.githubusercontent.com/rfortherestofus/mapping-with-r-v2/refs/heads/main/data/refugees.geojson") |>
ggplot() +
geom_sf(aes(fill = number_of_refugees))
```
:::