You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: resources/Mapping.Rmd
+33-1Lines changed: 33 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -22,6 +22,30 @@ Maps can be tricky in R! There are many packages to choose from.
22
22
- Some require API keys (e.g., `ggmap`, `tidycensus`)
23
23
- Some are interactive (e.g., `leaflet`)
24
24
25
+
## What does a map in R look like?
26
+
27
+
```{r message=FALSE, warning=FALSE, echo=FALSE}
28
+
library(tidyverse) # `geom_sf()` from ggplot2
29
+
library(tidycensus) # `get_acs()` function for American Community Survey data
30
+
31
+
wa_income <- get_acs(
32
+
geography = "tract",
33
+
variables = "B19013_001", # Income key
34
+
state = "WA",
35
+
year = 2022,
36
+
geometry = TRUE
37
+
)
38
+
39
+
ggplot(data = wa_income, aes(fill = estimate)) +
40
+
geom_sf() +
41
+
labs(title = "Median Household Income by Census Tract",
42
+
x = "Longitude",
43
+
y = "Latitude") +
44
+
theme_classic() +
45
+
scale_fill_viridis_c() +
46
+
theme(legend.title = element_blank())
47
+
```
48
+
25
49
## Data formats - boundary data
26
50
27
51
```{r message=FALSE}
@@ -53,10 +77,14 @@ These objects store geometric shapes (like points, lines, or polygons) along wit
53
77
54
78
the `geom` column is a `MULTIPOLYGON` — a geometry type representing complex shapes, which may consist of multiple polygons (e.g., islands or non-contiguous regions).
55
79
80
+
Federal Information Processing System (FIPS) Codes for States and Counties are numbers which uniquely identify geographic areas. See [this codebook](https://transition.fcc.gov/oet/info/maps/census/fips/fips.txt).
81
+
56
82
## `ggplot` has spatial functions{.codesmall}
57
83
58
84
`geom_polygon()` works with boundary data
59
85
86
+
Let's plot county outlines and major cities.
87
+
60
88
```{r message=FALSE}
61
89
library(tidyverse) # `map_data()` from ggplot2
62
90
library(maps) # `us.cities` data
@@ -101,6 +129,8 @@ title(main = "Washington State Cities")
101
129
102
130
## `usmap` is compatible with ggplot
103
131
132
+
Let's fill each county based on its population.
133
+
104
134
```{r message=FALSE}
105
135
library(tidyverse)
106
136
library(usmap) # `countypop` data and the `plot_usmap()` function
@@ -170,13 +200,15 @@ plot_4
170
200
171
201
Use `geom_sf()` function with SF data.
172
202
203
+
Let's fill each census tract by median household income.
204
+
173
205
```{r message=FALSE, warning=FALSE}
174
206
library(tidyverse) # `geom_sf()` from ggplot2
175
207
library(tidycensus) # `get_acs()` function for American Community Survey data
0 commit comments