-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector-maps-solution.Rmd
More file actions
129 lines (106 loc) · 3.22 KB
/
Copy pathvector-maps-solution.Rmd
File metadata and controls
129 lines (106 loc) · 3.22 KB
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
---
title: "Practice drawing vector maps"
output: html_document
---
```{r packages}
library(tidyverse)
library(sf)
library(colorspace) # for better color palettes
library(here) # for specifying directory location
theme_set(theme_minimal()) # could choose something better for maps
```
## Import information on median household income in 2020 for Cook County, IL at the tract-level using the ACS
```{r}
cook_county_path <- here("data", "cook-county-inc.geojson")
cook_inc <- st_read(dsn = cook_county_path)
cook_inc
```
## Draw a choropleth using the median household income data
- Use a continuous color gradient to identify each tract's median household income
```{r}
# use fill channel
ggplot(data = cook_inc) +
geom_sf(mapping = aes(fill = estimate)) +
scale_fill_continuous_sequential(palette = "viridis", rev = FALSE)
# use fill and color channels - what is the difference?
ggplot(data = cook_inc) +
geom_sf(mapping = aes(fill = estimate, color = estimate)) +
scale_fill_continuous_sequential(palette = "viridis", rev = FALSE, aesthetics = c("fill", "color"))
```
```{r}
# fully labeled
ggplot(data = cook_inc) +
geom_sf(mapping = aes(fill = estimate, color = estimate)) +
scale_fill_continuous_sequential(
palette = "viridis",
rev = FALSE,
aesthetics = c("fill", "color"),
labels = scales::label_dollar(),
name = NULL
) +
labs(
title = "Median household income in 2020",
subtitle = "Cook County, by census tract"
) +
ggthemes::theme_map()
```
## Draw the same choropleth for Cook County, but convert median household income into a discrete variable with 6 levels
- Using `cut_interval()`
```{r}
cook_inc %>%
mutate(inc_cut = cut_interval(estimate, n = 6)) %>%
ggplot() +
geom_sf(mapping = aes(fill = inc_cut, color = inc_cut)) +
scale_fill_discrete_sequential(palette = "viridis", rev = FALSE, aesthetics = c("fill", "color"))
```
- Using `cut_number()`
```{r}
cook_inc %>%
mutate(inc_cut = cut_number(estimate, n = 6)) %>%
ggplot() +
geom_sf(mapping = aes(fill = inc_cut, color = inc_cut)) +
scale_fill_discrete_sequential(palette = "viridis", rev = FALSE, aesthetics = c("fill", "color"))
```
- Using `binned_scale()`
```{r}
# default breaks
ggplot(data = cook_inc) +
geom_sf(mapping = aes(fill = estimate, color = estimate)) +
scale_fill_binned_sequential(
palette = "viridis",
rev = FALSE,
aesthetics = c("fill", "color"),
labels = scales::label_dollar()
)
# quartiles
ggplot(data = cook_inc) +
geom_sf(mapping = aes(fill = estimate, color = estimate)) +
scale_fill_binned_sequential(
palette = "viridis",
rev = FALSE,
aesthetics = c("fill", "color"),
n.breaks = 4, nice.breaks = FALSE,
labels = scales::label_dollar()
)
```
## Draw a choropleth of median household income for each US county
- Adjust to use Albers equal-area projection
```{r}
usa_inc <- st_read(dsn = here("data", "usa-inc.geojson"))
usa_inc
```
```{r}
ggplot(data = usa_inc) +
geom_sf(mapping = aes(fill = estimate, color = estimate)) +
scale_fill_continuous_sequential(
palette = "viridis",
rev = FALSE,
aesthetics = c("fill", "color"),
labels = scales::label_dollar()
) +
coord_sf(crs = 2163)
```
## Session Info
```{r}
sessioninfo::session_info()
```