Skip to content

Commit a9aa825

Browse files
committed
source commit: fc6b38f
0 parents  commit a9aa825

File tree

147 files changed

+11144
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

147 files changed

+11144
-0
lines changed

01-raster-structure.md

Lines changed: 821 additions & 0 deletions
Large diffs are not rendered by default.

02-raster-plot.md

Lines changed: 481 additions & 0 deletions
Large diffs are not rendered by default.

03-raster-reproject-in-r.md

Lines changed: 545 additions & 0 deletions
Large diffs are not rendered by default.

04-raster-calculations-in-r.md

Lines changed: 644 additions & 0 deletions
Large diffs are not rendered by default.

05-raster-multi-band-in-r.md

Lines changed: 946 additions & 0 deletions
Large diffs are not rendered by default.

06-vector-open-shapefile-in-r.md

Lines changed: 452 additions & 0 deletions
Large diffs are not rendered by default.

07-vector-shapefile-attributes-in-r.md

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

0 commit comments

Comments
 (0)