Skip to content

Commit bbc5b2c

Browse files
committed
Add st_join to Spatial Data 2
1 parent cf64c50 commit bbc5b2c

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

materials/spatial-data-polygon-aggregation-R.md

+18-2
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,31 @@ elevs_by_soil$harv_dtmfull.tif
4646
* I'll use `mutate` from `dplyr`
4747

4848
```r
49-
harv_soils <- mutate(harv_soils, elevation = elevs_by_soil$harv_dtmfull.tif)
49+
harv_soils_elevs <- mutate(harv_soils, elevation = elevs_by_soil$harv_dtmfull.tif)
5050
```
5151

52+
* Or we can use `st_join`
53+
* To do this we have to first convert `elevs_by_soil` into an `sf` object
54+
55+
```r
56+
elevs_by_soil <- st_as_sf(elevs_by_soil)
57+
```
58+
59+
* And then join
60+
61+
```r
62+
harv_soils_elevs <- st_join(harv_soils, elevs_by_soil, join = st_equals)
63+
```
64+
65+
* The `join = st_equals` indicates that we only want to match polygons that are the same
66+
* `st_join` defaults to using `st_intersects`, which means that any polygons that touch each other will be match
67+
5268
* Once we done this then we can make maps using this information
5369
* So let's plot the our soils map but colored by average elevation within the region
5470

5571
```r
5672
ggplot() +
57-
geom_sf(data = harv_soils, mapping = aes(fill = elevation)) +
73+
geom_sf(data = harv_soils_elevs, mapping = aes(fill = elevation)) +
5874
scale_fill_viridis_c()
5975
```
6076

0 commit comments

Comments
 (0)