Skip to content

Commit 1b933fe

Browse files
authored
Merge pull request #240 from fhdsl/codeathon
Lil tweaks to the mapping module to make it easier to follow
2 parents 148d809 + 63cc95e commit 1b933fe

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

resources/Mapping.Rmd

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,30 @@ Maps can be tricky in R! There are many packages to choose from.
2222
- Some require API keys (e.g., `ggmap`, `tidycensus`)
2323
- Some are interactive (e.g., `leaflet`)
2424

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+
2549
## Data formats - boundary data
2650

2751
```{r message=FALSE}
@@ -53,10 +77,14 @@ These objects store geometric shapes (like points, lines, or polygons) along wit
5377

5478
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).
5579

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+
5682
## `ggplot` has spatial functions{.codesmall}
5783

5884
`geom_polygon()` works with boundary data
5985

86+
Let's plot county outlines and major cities.
87+
6088
```{r message=FALSE}
6189
library(tidyverse) # `map_data()` from ggplot2
6290
library(maps) # `us.cities` data
@@ -101,6 +129,8 @@ title(main = "Washington State Cities")
101129

102130
## `usmap` is compatible with ggplot
103131

132+
Let's fill each county based on its population.
133+
104134
```{r message=FALSE}
105135
library(tidyverse)
106136
library(usmap) # `countypop` data and the `plot_usmap()` function
@@ -170,13 +200,15 @@ plot_4
170200

171201
Use `geom_sf()` function with SF data.
172202

203+
Let's fill each census tract by median household income.
204+
173205
```{r message=FALSE, warning=FALSE}
174206
library(tidyverse) # `geom_sf()` from ggplot2
175207
library(tidycensus) # `get_acs()` function for American Community Survey data
176208
177209
wa_income <- get_acs(
178210
geography = "tract",
179-
variables = "B19013_001", # Income key
211+
variables = "B19013_001", # Median income code
180212
state = "WA",
181213
year = 2022,
182214
geometry = TRUE

resources/Mapping.html

Lines changed: 15 additions & 3 deletions
Large diffs are not rendered by default.

resources/Mapping.pdf

592 KB
Binary file not shown.

0 commit comments

Comments
 (0)