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

Add st_join to Spatial Data 2 #1106

Merged
merged 1 commit into from
Nov 12, 2024
Merged
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
20 changes: 18 additions & 2 deletions materials/spatial-data-polygon-aggregation-R.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,31 @@ elevs_by_soil$harv_dtmfull.tif
* I'll use `mutate` from `dplyr`

```r
harv_soils <- mutate(harv_soils, elevation = elevs_by_soil$harv_dtmfull.tif)
harv_soils_elevs <- mutate(harv_soils, elevation = elevs_by_soil$harv_dtmfull.tif)
```

* Or we can use `st_join`
* To do this we have to first convert `elevs_by_soil` into an `sf` object

```r
elevs_by_soil <- st_as_sf(elevs_by_soil)
```

* And then join

```r
harv_soils_elevs <- st_join(harv_soils, elevs_by_soil, join = st_equals)
```

* The `join = st_equals` indicates that we only want to match polygons that are the same
* `st_join` defaults to using `st_intersects`, which means that any polygons that touch each other will be match

* Once we done this then we can make maps using this information
* So let's plot the our soils map but colored by average elevation within the region

```r
ggplot() +
geom_sf(data = harv_soils, mapping = aes(fill = elevation)) +
geom_sf(data = harv_soils_elevs, mapping = aes(fill = elevation)) +
scale_fill_viridis_c()
```

Expand Down
Loading