Skip to content

Commit 8268eb6

Browse files
committed
Adding file for lesson on creating a map of the us and it's territories
1 parent 8f716d9 commit 8268eb6

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
## Learning Objectives
2+
3+
- Introduce the `sf` package for working with spatial data in R
4+
- Create a U.S map including territories using `tigris` and `ggplot2`
5+
- Enhance and customize maps to create publication grade figures
6+
7+
8+
## Big Idea
9+
10+
The primary purpose of this workshop is to demonstrate how to use R to visualize spatial data. Particularly how to create a map of the U.S and all its territory. We will briefly review the basic concepts of working with geospatial data. Therefore we may not cover key concepts wehn doing spatial data analysis. For more in-depth learning on spatial analysis in R, you can check out the online book [“Geocomputation with R”](https://r.geocompx.org/) by Lovelace et al. (2024).
11+
12+
13+
## Introduction to spatial data with `sf`
14+
15+
From the sf vignette:
16+
17+
> Simple features or simple feature access refers to a formal standard (ISO 19125-1:2004) that describes how objects in the real world can be represented in computers, with emphasis on the spatial geometry of these objects. It also describes how such objects can be stored in and retrieved from databases, and which geometrical operations should be defined for them.
18+
19+
The sf package is an R implementation of Simple Features. This package incorporates:
20+
21+
- A new spatial data class system in R
22+
- Functions for reading and writing spatial data
23+
- Tools for spatial operations on vectors
24+
- Most of the functions in this package starts with prefix st_ which stands for spatial and temporal.
25+
26+
For this lesson we are going to polygons for the entire U.S by states including territories from the `tirgris` package AND **ADDD***
27+
28+
29+
::: column-margin
30+
![](images/tigris_sticker.png){fig-align="right"}
31+
`tigris` is an R package that allows users to directly download and use [TIGER/Line shapefiles](https://www.census.gov/geographies/mapping-files/time-series/geo/tiger-line-file.html) from the US Census Bureau.
32+
:::
33+
34+
35+
## About the Data
36+
37+
## Getting Started
38+
39+
:::callout-tip
40+
## Setup
41+
42+
- Create a new R project
43+
44+
- Open a new R Script (File > New File > R Script)
45+
46+
- Load the packages we will use for this session
47+
48+
```{r}
49+
library(tigris)
50+
library(sf)
51+
library(dplyr)
52+
library(ggplot2)
53+
library(here)
54+
```
55+
56+
- Save your R Script
57+
58+
:::
59+
60+
## Load and Read data
61+
62+
Read Ecoregion data
63+
64+
```{r}
65+
ecoregions_us <- read_sf(here("materials/data/shapefiles/Aggr_Ecoregions_2015.shp"))
66+
67+
st_crs(ecoregions_us)
68+
69+
```
70+
71+
72+
73+
We will first load the U.S states + territories polygons and then read in the ecoregion data.
74+
75+
76+
77+
```{r}
78+
us_states <- states(cb = TRUE,
79+
resolution = "20m") %>%
80+
shift_geometry()
81+
82+
us_states2 <- states()
83+
84+
continental_us <- us_states2 %>%
85+
filter(!NAME %in% c("Alaska", "American Samoa", "Commonwealth of the Northern Mariana Islands", "Guam", "Hawaii", "Puerto Rico", "United States Virgin Islands", "District of Columbia"))
86+
87+
class(us_states)
88+
st_crs(us_states)
89+
90+
91+
```
92+
93+
94+
## plotting multiple layers
95+
```{r}
96+
ggplot()+
97+
geom_sf(data = ecoregions_us,
98+
aes(fill = WSA9_NAME))+
99+
geom_sf(data = continental_us)+
100+
scale_fill_viridis_d()+
101+
theme_bw()
102+
103+
```
104+
105+
106+
107+
## Bonus
108+
109+
CREATE A GITHUB REPO VERSION OF THIS PROJECT!
110+
111+

0 commit comments

Comments
 (0)