You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
60
60
61
61
## Load the Data
62
62
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`
64
64
package also allows us to explore metadata using similar commands for both
65
65
raster and vector files. Make sure that you have these packages loaded.
66
66
@@ -69,18 +69,18 @@ We will continue to work with the three shapefiles that we loaded in the
69
69
70
70
## Plotting Multiple Shapefiles
71
71
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?
77
77
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
80
80
will need to build a custom legend as well.
81
81
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 `+`.
84
84
85
85
```{r plot-many-shapefiles}
86
86
ggplot() +
@@ -91,7 +91,12 @@ ggplot() +
91
91
coord_sf()
92
92
```
93
93
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'`.
95
100
96
101
```{r plot-custom-shape}
97
102
ggplot() +
@@ -105,7 +110,8 @@ ggplot() +
105
110
coord_sf()
106
111
```
107
112
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.
109
115
110
116
```{r create-custom-legend}
111
117
ggplot() +
@@ -119,7 +125,9 @@ ggplot() +
119
125
coord_sf()
120
126
```
121
127
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.
123
131
124
132
::::::::::::::::::::::::::::::::::::::::: callout
125
133
@@ -148,29 +156,31 @@ ggplot() +
148
156
## Challenge: Plot Polygon by Attribute
149
157
150
158
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.
155
164
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.
158
167
159
168
::::::::::::::: solution
160
169
161
170
## Answers
162
171
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
0 commit comments