Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lil tweaks to the mapping module to make it easier to follow #240

Merged
merged 1 commit into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion resources/Mapping.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,30 @@ Maps can be tricky in R! There are many packages to choose from.
- Some require API keys (e.g., `ggmap`, `tidycensus`)
- Some are interactive (e.g., `leaflet`)

## What does a map in R look like?

```{r message=FALSE, warning=FALSE, echo=FALSE}
library(tidyverse) # `geom_sf()` from ggplot2
library(tidycensus) # `get_acs()` function for American Community Survey data

wa_income <- get_acs(
geography = "tract",
variables = "B19013_001", # Income key
state = "WA",
year = 2022,
geometry = TRUE
)

ggplot(data = wa_income, aes(fill = estimate)) +
geom_sf() +
labs(title = "Median Household Income by Census Tract",
x = "Longitude",
y = "Latitude") +
theme_classic() +
scale_fill_viridis_c() +
theme(legend.title = element_blank())
```

## Data formats - boundary data

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

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

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

## `ggplot` has spatial functions{.codesmall}

`geom_polygon()` works with boundary data

Let's plot county outlines and major cities.

```{r message=FALSE}
library(tidyverse) # `map_data()` from ggplot2
library(maps) # `us.cities` data
Expand Down Expand Up @@ -101,6 +129,8 @@ title(main = "Washington State Cities")

## `usmap` is compatible with ggplot

Let's fill each county based on its population.

```{r message=FALSE}
library(tidyverse)
library(usmap) # `countypop` data and the `plot_usmap()` function
Expand Down Expand Up @@ -170,13 +200,15 @@ plot_4

Use `geom_sf()` function with SF data.

Let's fill each census tract by median household income.

```{r message=FALSE, warning=FALSE}
library(tidyverse) # `geom_sf()` from ggplot2
library(tidycensus) # `get_acs()` function for American Community Survey data

wa_income <- get_acs(
geography = "tract",
variables = "B19013_001", # Income key
variables = "B19013_001", # Median income code
state = "WA",
year = 2022,
geometry = TRUE
Expand Down
18 changes: 15 additions & 3 deletions resources/Mapping.html

Large diffs are not rendered by default.

Binary file modified resources/Mapping.pdf
Binary file not shown.
Loading