Skip to content

Commit

Permalink
Episode 8 updated by replacing calls to raster and rgdal packages wit…
Browse files Browse the repository at this point in the history
…h calls to terra package. datacarpentry#368 datacarpentry#363
  • Loading branch information
albhasan committed Mar 14, 2023
1 parent 231af26 commit b860ef0
Showing 1 changed file with 51 additions and 40 deletions.
91 changes: 51 additions & 40 deletions episodes/08-vector-plot-shapefiles-custom-legend.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ source("setup.R")
::::::::::::::::::::::::::::::::::::::::::::::::::

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

## Things You'll Need To Complete This Episode

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


::::::::::::::::::::::::::::::::::::::::::::::::::

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

## Load the Data

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

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

## Plotting Multiple Shapefiles

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

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

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

```{r plot-many-shapefiles}
ggplot() +
Expand All @@ -91,7 +91,12 @@ ggplot() +
coord_sf()
```

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'`.
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'`.

```{r plot-custom-shape}
ggplot() +
Expand All @@ -105,7 +110,8 @@ ggplot() +
coord_sf()
```

Now lets adjust the legend titles by passing a `name` to the respective `color` and `fill` palettes.
Now lets adjust the legend titles by passing a `name` to the respective `color`
and `fill` palettes.

```{r create-custom-legend}
ggplot() +
Expand All @@ -119,7 +125,9 @@ ggplot() +
coord_sf()
```

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

::::::::::::::::::::::::::::::::::::::::: callout

Expand Down Expand Up @@ -148,29 +156,31 @@ ggplot() +
## Challenge: Plot Polygon by Attribute

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

2. Modify the plot above. Tell R to plot each point, using a different
symbol of `shape` value.
2. Modify the plot above. Tell R to plot each point, using a different symbol
of `shape` value.

::::::::::::::: solution

## Answers

First we need to read in the data and see how many
unique soils are represented in the `soilTypeOr` attribute.
First we need to read in the data and see how many unique soils are represented
in the `soilTypeOr` attribute.

```{r}
plot_locations <- st_read("data/NEON-DS-Site-Layout-Files/HARV/PlotLocations_HARV.shp")
plot_locations <-
st_read("data/NEON-DS-Site-Layout-Files/HARV/PlotLocations_HARV.shp")
plot_locations$soilTypeOr <- as.factor(plot_locations$soilTypeOr)
levels(plot_locations$soilTypeOr)
```

Next we can create a new color palette with one color for
each soil type.
Next we can create a new color palette with one color for each soil type.

```{r}
blue_orange <- c("cornflowerblue", "darkorange")
Expand All @@ -184,15 +194,17 @@ ggplot() +
geom_sf(data = plot_locations, aes(fill = soilTypeOr),
shape = 21, show.legend = 'point') +
scale_color_manual(name = "Line Type", values = road_colors,
guide = guide_legend(override.aes = list(linetype = "solid", shape = NA))) +
guide = guide_legend(override.aes = list(linetype = "solid",
shape = NA))) +
scale_fill_manual(name = "Soil Type", values = blue_orange,
guide = guide_legend(override.aes = list(linetype = "blank", shape = 21, colour = NA))) +
guide = guide_legend(override.aes = list(linetype = "blank", shape = 21,
colour = NA))) +
ggtitle("NEON Harvard Forest Field Site") +
coord_sf()
```

If we want each soil to be shown with a different symbol, we can
give multiple values to the `scale_shape_manual()` argument.
If we want each soil to be shown with a different symbol, we can give multiple
values to the `scale_shape_manual()` argument.

```{r harv-plot-locations-pch}
ggplot() +
Expand All @@ -217,11 +229,10 @@ ggplot() +

## Challenge: Plot Raster \& Vector Data Together

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

- The study site AOI.
- Roads.
Expand Down

0 comments on commit b860ef0

Please sign in to comment.