Skip to content

Commit 1b69ae7

Browse files
authored
Merge pull request #393 from albhasan/m2022q1_e8
Episode 8 updated by replacing calls to raster and rgdal packages
2 parents 2be64c8 + b860ef0 commit 1b69ae7

File tree

1 file changed

+51
-40
lines changed

1 file changed

+51
-40
lines changed

episodes/08-vector-plot-shapefiles-custom-legend.Rmd

Lines changed: 51 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ source("setup.R")
2525
::::::::::::::::::::::::::::::::::::::::::::::::::
2626

2727
```{r load-libraries, echo=FALSE, results="hide", message=FALSE}
28-
library(raster)
29-
library(rgdal)
28+
library(terra)
3029
library(ggplot2)
3130
library(dplyr)
3231
library(sf)
@@ -37,7 +36,7 @@ library(sf)
3736
aoi_boundary_HARV <- st_read("data/NEON-DS-Site-Layout-Files/HARV/HarClip_UTMZ18.shp")
3837
lines_HARV <- st_read("data/NEON-DS-Site-Layout-Files/HARV/HARV_roads.shp")
3938
point_HARV <- st_read("data/NEON-DS-Site-Layout-Files/HARV/HARVtower_UTM18N.shp")
40-
CHM_HARV <- raster("data/NEON-DS-Airborne-Remote-Sensing/HARV/CHM/HARV_chmCrop.tif")
39+
CHM_HARV <- rast("data/NEON-DS-Airborne-Remote-Sensing/HARV/CHM/HARV_chmCrop.tif")
4140
CHM_HARV_df <- as.data.frame(CHM_HARV, xy = TRUE)
4241
road_colors <- c("blue", "green", "navy", "purple")
4342
```
@@ -46,21 +45,22 @@ road_colors <- c("blue", "green", "navy", "purple")
4645

4746
## Things You'll Need To Complete This Episode
4847

49-
See the [lesson homepage](.) for detailed information about the software,
50-
data, and other prerequisites you will need to work through the examples in this episode.
48+
See the [lesson homepage](.) for detailed information about the software, data,
49+
and other prerequisites you will need to work through the examples in this
50+
episode.
5151

5252

5353
::::::::::::::::::::::::::::::::::::::::::::::::::
5454

5555
This episode builds upon
5656
[the previous episode](07-vector-shapefile-attributes-in-r/)
5757
to work with shapefile attributes in R and explores how to plot multiple
58-
shapefiles. It also covers how to plot raster and vector data together
59-
on the same plot.
58+
shapefiles. It also covers how to plot raster and vector data together on the
59+
same plot.
6060

6161
## Load the Data
6262

63-
To work with vector data in R, we can use the `sf` library. The `raster`
63+
To work with vector data in R, we can use the `sf` library. The `terra`
6464
package also allows us to explore metadata using similar commands for both
6565
raster and vector files. Make sure that you have these packages loaded.
6666

@@ -69,18 +69,18 @@ We will continue to work with the three shapefiles that we loaded in the
6969

7070
## Plotting Multiple Shapefiles
7171

72-
In the [previous episode](07-vector-shapefile-attributes-in-r/),
73-
we learned how to plot information from a single shapefile and do
74-
some plot customization including adding a custom legend. However,
75-
what if we want to create a more complex plot with many shapefiles
76-
and unique symbols that need to be represented clearly in a legend?
72+
In the [previous episode](07-vector-shapefile-attributes-in-r/), we learned how
73+
to plot information from a single shapefile and do some plot customization
74+
including adding a custom legend. However, what if we want to create a more
75+
complex plot with many shapefiles and unique symbols that need to be
76+
represented clearly in a legend?
7777

78-
Now, let's create a plot that combines our tower location (`point_HARV`),
79-
site boundary (`aoi_boundary_HARV`) and roads (`lines_HARV`) spatial objects. We
78+
Now, let's create a plot that combines our tower location (`point_HARV`), site
79+
boundary (`aoi_boundary_HARV`) and roads (`lines_HARV`) spatial objects. We
8080
will need to build a custom legend as well.
8181

82-
To begin, we will create a plot with the site boundary as the first layer. Then layer
83-
the tower location and road data on top using `+`.
82+
To begin, we will create a plot with the site boundary as the first layer. Then
83+
layer the tower location and road data on top using `+`.
8484

8585
```{r plot-many-shapefiles}
8686
ggplot() +
@@ -91,7 +91,12 @@ ggplot() +
9191
coord_sf()
9292
```
9393

94-
Next, let's build a custom legend using the symbology (the colors and symbols) that we used to create the plot above. For example, it might be good if the lines were symbolized as lines. In the previous episode, you may have noticed that the default legend behavior for `geom_sf` is to draw a 'patch' for each legend entry. If you want the legend to draw lines or points, you need to add an instruction to the `geom_sf` call - in this case, `show.legend = 'line'`.
94+
Next, let's build a custom legend using the symbology (the colors and symbols)
95+
that we used to create the plot above. For example, it might be good if the
96+
lines were symbolized as lines. In the previous episode, you may have noticed
97+
that the default legend behavior for `geom_sf` is to draw a 'patch' for each
98+
legend entry. If you want the legend to draw lines or points, you need to add
99+
an instruction to the `geom_sf` call - in this case, `show.legend = 'line'`.
95100

96101
```{r plot-custom-shape}
97102
ggplot() +
@@ -105,7 +110,8 @@ ggplot() +
105110
coord_sf()
106111
```
107112

108-
Now lets adjust the legend titles by passing a `name` to the respective `color` and `fill` palettes.
113+
Now lets adjust the legend titles by passing a `name` to the respective `color`
114+
and `fill` palettes.
109115

110116
```{r create-custom-legend}
111117
ggplot() +
@@ -119,7 +125,9 @@ ggplot() +
119125
coord_sf()
120126
```
121127

122-
Finally, it might be better if the points were symbolized as a symbol. We can customize this using `shape` parameters in our call to `geom_sf`: 16 is a point symbol, 15 is a box.
128+
Finally, it might be better if the points were symbolized as a symbol. We can
129+
customize this using `shape` parameters in our call to `geom_sf`: 16 is a point
130+
symbol, 15 is a box.
123131

124132
::::::::::::::::::::::::::::::::::::::::: callout
125133

@@ -148,29 +156,31 @@ ggplot() +
148156
## Challenge: Plot Polygon by Attribute
149157

150158
1. Using the `NEON-DS-Site-Layout-Files/HARV/PlotLocations_HARV.shp` shapefile,
151-
create a map of study plot locations, with each point colored by the soil type
152-
(`soilTypeOr`). How many different soil types are there at this particular field
153-
site? Overlay this layer on top of the `lines_HARV` layer (the roads). Create a
154-
custom legend that applies line symbols to lines and point symbols to the points.
159+
create a map of study plot locations, with each point colored by the soil
160+
type (`soilTypeOr`). How many different soil types are there at this
161+
particular field site? Overlay this layer on top of the `lines_HARV` layer
162+
(the roads). Create a custom legend that applies line symbols to lines and
163+
point symbols to the points.
155164

156-
2. Modify the plot above. Tell R to plot each point, using a different
157-
symbol of `shape` value.
165+
2. Modify the plot above. Tell R to plot each point, using a different symbol
166+
of `shape` value.
158167

159168
::::::::::::::: solution
160169

161170
## Answers
162171

163-
First we need to read in the data and see how many
164-
unique soils are represented in the `soilTypeOr` attribute.
172+
First we need to read in the data and see how many unique soils are represented
173+
in the `soilTypeOr` attribute.
165174

166175
```{r}
167-
plot_locations <- st_read("data/NEON-DS-Site-Layout-Files/HARV/PlotLocations_HARV.shp")
176+
plot_locations <-
177+
st_read("data/NEON-DS-Site-Layout-Files/HARV/PlotLocations_HARV.shp")
168178
179+
plot_locations$soilTypeOr <- as.factor(plot_locations$soilTypeOr)
169180
levels(plot_locations$soilTypeOr)
170181
```
171182

172-
Next we can create a new color palette with one color for
173-
each soil type.
183+
Next we can create a new color palette with one color for each soil type.
174184

175185
```{r}
176186
blue_orange <- c("cornflowerblue", "darkorange")
@@ -184,15 +194,17 @@ ggplot() +
184194
geom_sf(data = plot_locations, aes(fill = soilTypeOr),
185195
shape = 21, show.legend = 'point') +
186196
scale_color_manual(name = "Line Type", values = road_colors,
187-
guide = guide_legend(override.aes = list(linetype = "solid", shape = NA))) +
197+
guide = guide_legend(override.aes = list(linetype = "solid",
198+
shape = NA))) +
188199
scale_fill_manual(name = "Soil Type", values = blue_orange,
189-
guide = guide_legend(override.aes = list(linetype = "blank", shape = 21, colour = NA))) +
200+
guide = guide_legend(override.aes = list(linetype = "blank", shape = 21,
201+
colour = NA))) +
190202
ggtitle("NEON Harvard Forest Field Site") +
191203
coord_sf()
192204
```
193205

194-
If we want each soil to be shown with a different symbol, we can
195-
give multiple values to the `scale_shape_manual()` argument.
206+
If we want each soil to be shown with a different symbol, we can give multiple
207+
values to the `scale_shape_manual()` argument.
196208

197209
```{r harv-plot-locations-pch}
198210
ggplot() +
@@ -217,11 +229,10 @@ ggplot() +
217229

218230
## Challenge: Plot Raster \& Vector Data Together
219231

220-
You can plot vector data layered on top of raster data using the
221-
`+` to add a layer in `ggplot`. Create a plot that uses the NEON AOI
222-
Canopy Height Model `data/NEON-DS-Airborne-Remote-Sensing/HARV/CHM/HARV_chmCrop.tif`
223-
as a base layer. On top of the
224-
CHM, please add:
232+
You can plot vector data layered on top of raster data using the `+` to add a
233+
layer in `ggplot`. Create a plot that uses the NEON AOI Canopy Height Model
234+
`data/NEON-DS-Airborne-Remote-Sensing/HARV/CHM/HARV_chmCrop.tif` as a base
235+
layer. On top of the CHM, please add:
225236

226237
- The study site AOI.
227238
- Roads.

0 commit comments

Comments
 (0)