|
| 1 | +--- |
| 2 | +title: Plot Multiple Vector Layers |
| 3 | +teaching: 40 |
| 4 | +exercises: 20 |
| 5 | +source: Rmd |
| 6 | +--- |
| 7 | + |
| 8 | + |
| 9 | +```{.warning} |
| 10 | +Warning in |
| 11 | +download.file("https://www.naturalearthdata.com/http//www.naturalearthdata.com/download/110m/physical/ne_110m_graticules_all.zip", |
| 12 | +: cannot open URL |
| 13 | +'https://www.naturalearthdata.com/http//www.naturalearthdata.com/download/110m/physical/ne_110m_graticules_all.zip': |
| 14 | +HTTP status was '500 Internal Server Error' |
| 15 | +``` |
| 16 | + |
| 17 | +```{.error} |
| 18 | +Error in download.file("https://www.naturalearthdata.com/http//www.naturalearthdata.com/download/110m/physical/ne_110m_graticules_all.zip", : cannot open URL 'https://www.naturalearthdata.com/http//www.naturalearthdata.com/download/110m/physical/ne_110m_graticules_all.zip' |
| 19 | +``` |
| 20 | + |
| 21 | +::::::::::::::::::::::::::::::::::::::: objectives |
| 22 | + |
| 23 | +- Plot multiple vector layers in the same plot. |
| 24 | +- Apply custom symbols to spatial objects in a plot. |
| 25 | +- Create a multi-layered plot with raster and vector data. |
| 26 | + |
| 27 | +:::::::::::::::::::::::::::::::::::::::::::::::::: |
| 28 | + |
| 29 | +:::::::::::::::::::::::::::::::::::::::: questions |
| 30 | + |
| 31 | +- How can I create map compositions with custom legends using ggplot? |
| 32 | +- How can I plot raster and vector data together? |
| 33 | + |
| 34 | +:::::::::::::::::::::::::::::::::::::::::::::::::: |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | +:::::::::::::::::::::::::::::::::::::::::: prereq |
| 41 | + |
| 42 | +## Things You'll Need To Complete This Episode |
| 43 | + |
| 44 | +See the [lesson homepage](.) for detailed information about the software, data, |
| 45 | +and other prerequisites you will need to work through the examples in this |
| 46 | +episode. |
| 47 | + |
| 48 | + |
| 49 | +:::::::::::::::::::::::::::::::::::::::::::::::::: |
| 50 | + |
| 51 | +This episode builds upon |
| 52 | +[the previous episode](07-vector-shapefile-attributes-in-r/) |
| 53 | +to work with vector layers in R and explore how to plot multiple |
| 54 | +vector layers. It also covers how to plot raster and vector data together on the |
| 55 | +same plot. |
| 56 | + |
| 57 | +## Load the Data |
| 58 | + |
| 59 | +To work with vector data in R, we can use the `sf` library. The `terra` |
| 60 | +package also allows us to explore metadata using similar commands for both |
| 61 | +raster and vector files. Make sure that you have these packages loaded. |
| 62 | + |
| 63 | +We will continue to work with the three ESRI `shapefile` that we loaded in the |
| 64 | +[Open and Plot Vector Layers in R](06-vector-open-shapefile-in-r/) episode. |
| 65 | + |
| 66 | +## Plotting Multiple Vector Layers |
| 67 | + |
| 68 | +In the [previous episode](07-vector-shapefile-attributes-in-r/), we learned how |
| 69 | +to plot information from a single vector layer and do some plot customization |
| 70 | +including adding a custom legend. However, what if we want to create a more |
| 71 | +complex plot with many vector layers and unique symbols that need to be |
| 72 | +represented clearly in a legend? |
| 73 | + |
| 74 | +Now, let's create a plot that combines our tower location (`point_HARV`), site |
| 75 | +boundary (`aoi_boundary_HARV`) and roads (`lines_HARV`) spatial objects. We |
| 76 | +will need to build a custom legend as well. |
| 77 | + |
| 78 | +To begin, we will create a plot with the site boundary as the first layer. Then |
| 79 | +layer the tower location and road data on top using `+`. |
| 80 | + |
| 81 | + |
| 82 | +```r |
| 83 | +ggplot() + |
| 84 | + geom_sf(data = aoi_boundary_HARV, fill = "grey", color = "grey") + |
| 85 | + geom_sf(data = lines_HARV, aes(color = TYPE), size = 1) + |
| 86 | + geom_sf(data = point_HARV) + |
| 87 | + ggtitle("NEON Harvard Forest Field Site") + |
| 88 | + coord_sf() |
| 89 | +``` |
| 90 | + |
| 91 | +<img src="fig/08-vector-plot-shapefiles-custom-legend-rendered-plot-many-shapefiles-1.png" style="display: block; margin: auto;" /> |
| 92 | + |
| 93 | +Next, let's build a custom legend using the symbology (the colors and symbols) |
| 94 | +that we used to create the plot above. For example, it might be good if the |
| 95 | +lines were symbolized as lines. In the previous episode, you may have noticed |
| 96 | +that the default legend behavior for `geom_sf` is to draw a 'patch' for each |
| 97 | +legend entry. If you want the legend to draw lines or points, you need to add |
| 98 | +an instruction to the `geom_sf` call - in this case, `show.legend = 'line'`. |
| 99 | + |
| 100 | + |
| 101 | +```r |
| 102 | +ggplot() + |
| 103 | + geom_sf(data = aoi_boundary_HARV, fill = "grey", color = "grey") + |
| 104 | + geom_sf(data = lines_HARV, aes(color = TYPE), |
| 105 | + show.legend = "line", size = 1) + |
| 106 | + geom_sf(data = point_HARV, aes(fill = Sub_Type), color = "black") + |
| 107 | + scale_color_manual(values = road_colors) + |
| 108 | + scale_fill_manual(values = "black") + |
| 109 | + ggtitle("NEON Harvard Forest Field Site") + |
| 110 | + coord_sf() |
| 111 | +``` |
| 112 | + |
| 113 | +<img src="fig/08-vector-plot-shapefiles-custom-legend-rendered-plot-custom-shape-1.png" style="display: block; margin: auto;" /> |
| 114 | + |
| 115 | +Now lets adjust the legend titles by passing a `name` to the respective `color` |
| 116 | +and `fill` palettes. |
| 117 | + |
| 118 | + |
| 119 | +```r |
| 120 | +ggplot() + |
| 121 | + geom_sf(data = aoi_boundary_HARV, fill = "grey", color = "grey") + |
| 122 | + geom_sf(data = point_HARV, aes(fill = Sub_Type)) + |
| 123 | + geom_sf(data = lines_HARV, aes(color = TYPE), show.legend = "line", |
| 124 | + size = 1) + |
| 125 | + scale_color_manual(values = road_colors, name = "Line Type") + |
| 126 | + scale_fill_manual(values = "black", name = "Tower Location") + |
| 127 | + ggtitle("NEON Harvard Forest Field Site") + |
| 128 | + coord_sf() |
| 129 | +``` |
| 130 | + |
| 131 | +<img src="fig/08-vector-plot-shapefiles-custom-legend-rendered-create-custom-legend-1.png" style="display: block; margin: auto;" /> |
| 132 | + |
| 133 | +Finally, it might be better if the points were symbolized as a symbol. We can |
| 134 | +customize this using `shape` parameters in our call to `geom_sf`: 16 is a point |
| 135 | +symbol, 15 is a box. |
| 136 | + |
| 137 | +::::::::::::::::::::::::::::::::::::::::: callout |
| 138 | + |
| 139 | +## Data Tip |
| 140 | + |
| 141 | +To view a short list of `shape` symbols, |
| 142 | +type `?pch` into the R console. |
| 143 | + |
| 144 | + |
| 145 | +:::::::::::::::::::::::::::::::::::::::::::::::::: |
| 146 | + |
| 147 | + |
| 148 | +```r |
| 149 | +ggplot() + |
| 150 | + geom_sf(data = aoi_boundary_HARV, fill = "grey", color = "grey") + |
| 151 | + geom_sf(data = point_HARV, aes(fill = Sub_Type), shape = 15) + |
| 152 | + geom_sf(data = lines_HARV, aes(color = TYPE), |
| 153 | + show.legend = "line", size = 1) + |
| 154 | + scale_color_manual(values = road_colors, name = "Line Type") + |
| 155 | + scale_fill_manual(values = "black", name = "Tower Location") + |
| 156 | + ggtitle("NEON Harvard Forest Field Site") + |
| 157 | + coord_sf() |
| 158 | +``` |
| 159 | + |
| 160 | +<img src="fig/08-vector-plot-shapefiles-custom-legend-rendered-custom-symbols-1.png" style="display: block; margin: auto;" /> |
| 161 | + |
| 162 | +::::::::::::::::::::::::::::::::::::::: challenge |
| 163 | + |
| 164 | +## Challenge: Plot Polygon by Attribute |
| 165 | + |
| 166 | +1. Using the `NEON-DS-Site-Layout-Files/HARV/PlotLocations_HARV.shp` ESRI `shapefile`, |
| 167 | + create a map of study plot locations, with each point colored by the soil |
| 168 | + type (`soilTypeOr`). How many different soil types are there at this |
| 169 | + particular field site? Overlay this layer on top of the `lines_HARV` layer |
| 170 | + (the roads). Create a custom legend that applies line symbols to lines and |
| 171 | + point symbols to the points. |
| 172 | + |
| 173 | +2. Modify the plot above. Tell R to plot each point, using a different symbol |
| 174 | + of `shape` value. |
| 175 | + |
| 176 | +::::::::::::::: solution |
| 177 | + |
| 178 | +## Answers |
| 179 | + |
| 180 | +First we need to read in the data and see how many unique soils are represented |
| 181 | +in the `soilTypeOr` attribute. |
| 182 | + |
| 183 | + |
| 184 | +```r |
| 185 | +plot_locations <- |
| 186 | + st_read("data/NEON-DS-Site-Layout-Files/HARV/PlotLocations_HARV.shp") |
| 187 | +``` |
| 188 | + |
| 189 | +```{.output} |
| 190 | +Reading layer `PlotLocations_HARV' from data source |
| 191 | + `/home/runner/work/r-raster-vector-geospatial/r-raster-vector-geospatial/site/built/data/NEON-DS-Site-Layout-Files/HARV/PlotLocations_HARV.shp' |
| 192 | + using driver `ESRI Shapefile' |
| 193 | +Simple feature collection with 21 features and 25 fields |
| 194 | +Geometry type: POINT |
| 195 | +Dimension: XY |
| 196 | +Bounding box: xmin: 731405.3 ymin: 4712845 xmax: 732275.3 ymax: 4713846 |
| 197 | +Projected CRS: WGS 84 / UTM zone 18N |
| 198 | +``` |
| 199 | + |
| 200 | +```r |
| 201 | +plot_locations$soilTypeOr <- as.factor(plot_locations$soilTypeOr) |
| 202 | +levels(plot_locations$soilTypeOr) |
| 203 | +``` |
| 204 | + |
| 205 | +```{.output} |
| 206 | +[1] "Histosols" "Inceptisols" |
| 207 | +``` |
| 208 | + |
| 209 | +Next we can create a new color palette with one color for each soil type. |
| 210 | + |
| 211 | + |
| 212 | +```r |
| 213 | +blue_orange <- c("cornflowerblue", "darkorange") |
| 214 | +``` |
| 215 | + |
| 216 | +Finally, we will create our plot. |
| 217 | + |
| 218 | + |
| 219 | +```r |
| 220 | +ggplot() + |
| 221 | + geom_sf(data = lines_HARV, aes(color = TYPE), show.legend = "line") + |
| 222 | + geom_sf(data = plot_locations, aes(fill = soilTypeOr), |
| 223 | + shape = 21, show.legend = 'point') + |
| 224 | + scale_color_manual(name = "Line Type", values = road_colors, |
| 225 | + guide = guide_legend(override.aes = list(linetype = "solid", |
| 226 | + shape = NA))) + |
| 227 | + scale_fill_manual(name = "Soil Type", values = blue_orange, |
| 228 | + guide = guide_legend(override.aes = list(linetype = "blank", shape = 21, |
| 229 | + colour = NA))) + |
| 230 | + ggtitle("NEON Harvard Forest Field Site") + |
| 231 | + coord_sf() |
| 232 | +``` |
| 233 | + |
| 234 | +<img src="fig/08-vector-plot-shapefiles-custom-legend-rendered-harv-plot-locations-bg-1.png" style="display: block; margin: auto;" /> |
| 235 | + |
| 236 | +If we want each soil to be shown with a different symbol, we can give multiple |
| 237 | +values to the `scale_shape_manual()` argument. |
| 238 | + |
| 239 | + |
| 240 | +```r |
| 241 | +ggplot() + |
| 242 | + geom_sf(data = lines_HARV, aes(color = TYPE), show.legend = "line", size = 1) + |
| 243 | + geom_sf(data = plot_locations, aes(fill = soilTypeOr, shape = soilTypeOr), |
| 244 | + show.legend = 'point', size = 3) + |
| 245 | + scale_shape_manual(name = "Soil Type", values = c(21, 22)) + |
| 246 | + scale_color_manual(name = "Line Type", values = road_colors, |
| 247 | + guide = guide_legend(override.aes = list(linetype = "solid", shape = NA))) + |
| 248 | + scale_fill_manual(name = "Soil Type", values = blue_orange, |
| 249 | + guide = guide_legend(override.aes = list(linetype = "blank", shape = c(21, 22), |
| 250 | + color = blue_orange))) + |
| 251 | + ggtitle("NEON Harvard Forest Field Site") + |
| 252 | + coord_sf() |
| 253 | +``` |
| 254 | + |
| 255 | +<img src="fig/08-vector-plot-shapefiles-custom-legend-rendered-harv-plot-locations-pch-1.png" style="display: block; margin: auto;" /> |
| 256 | + |
| 257 | +::::::::::::::::::::::::: |
| 258 | + |
| 259 | +:::::::::::::::::::::::::::::::::::::::::::::::::: |
| 260 | + |
| 261 | +::::::::::::::::::::::::::::::::::::::: challenge |
| 262 | + |
| 263 | +## Challenge: Plot Raster \& Vector Data Together |
| 264 | + |
| 265 | +You can plot vector data layered on top of raster data using the `+` to add a |
| 266 | +layer in `ggplot`. Create a plot that uses the NEON AOI Canopy Height Model |
| 267 | +`data/NEON-DS-Airborne-Remote-Sensing/HARV/CHM/HARV_chmCrop.tif` as a base |
| 268 | +layer. On top of the CHM, please add: |
| 269 | + |
| 270 | +- The study site AOI. |
| 271 | +- Roads. |
| 272 | +- The tower location. |
| 273 | + |
| 274 | +Be sure to give your plot a meaningful title. |
| 275 | + |
| 276 | +::::::::::::::: solution |
| 277 | + |
| 278 | +## Answers |
| 279 | + |
| 280 | + |
| 281 | +```r |
| 282 | +ggplot() + |
| 283 | + geom_raster(data = CHM_HARV_df, aes(x = x, y = y, fill = HARV_chmCrop)) + |
| 284 | + geom_sf(data = lines_HARV, color = "black") + |
| 285 | + geom_sf(data = aoi_boundary_HARV, color = "grey20", size = 1) + |
| 286 | + geom_sf(data = point_HARV, pch = 8) + |
| 287 | + ggtitle("NEON Harvard Forest Field Site w/ Canopy Height Model") + |
| 288 | + coord_sf() |
| 289 | +``` |
| 290 | + |
| 291 | +<img src="fig/08-vector-plot-shapefiles-custom-legend-rendered-challenge-vector-raster-overlay-1.png" style="display: block; margin: auto;" /> |
| 292 | + |
| 293 | +::::::::::::::::::::::::: |
| 294 | + |
| 295 | +:::::::::::::::::::::::::::::::::::::::::::::::::: |
| 296 | + |
| 297 | + |
| 298 | + |
| 299 | +:::::::::::::::::::::::::::::::::::::::: keypoints |
| 300 | + |
| 301 | +- Use the `+` operator to add multiple layers to a ggplot. |
| 302 | +- Multi-layered plots can combine raster and vector datasets. |
| 303 | +- Use the `show.legend` argument to set legend symbol types. |
| 304 | +- Use the `scale_fill_manual()` function to set legend colors. |
| 305 | + |
| 306 | +:::::::::::::::::::::::::::::::::::::::::::::::::: |
| 307 | + |
| 308 | + |
0 commit comments