@@ -48,6 +48,7 @@ and other prerequisites you will need to work through the examples in this
48
48
episode.
49
49
50
50
51
+
51
52
::::::::::::::::::::::::::::::::::::::::::::::::::
52
53
53
54
This episode continues our discussion of vector layer attributes and covers how
@@ -58,7 +59,7 @@ attribute values.
58
59
59
60
## Load the Data
60
61
61
- We will continue using the ` sf ` , ` terra ` and ` ggplot2 ` packages in this
62
+ We will continue using the ` sf ` , ` terra ` ` dplyr ` and ` ggplot2 ` packages in this
62
63
episode. Make sure that you have these packages loaded. We will continue to
63
64
work with the three ESRI ` shapefiles ` (vector layers) that we loaded in the
64
65
[ Open and Plot Vector Layers in R] ( 06-vector-open-shapefile-in-r/ ) episode.
@@ -86,7 +87,7 @@ point_HARV
86
87
87
88
We can use the ` ncol ` function to count the number of attributes associated
88
89
with a spatial object too. Note that the geometry is just another column and
89
- counts towards the total.
90
+ counts towards the total. Let's look at the roads file:
90
91
91
92
``` {r shapefile-attributes}
92
93
ncol(lines_HARV)
@@ -215,7 +216,7 @@ connecting line thickness to a data variable.
215
216
216
217
``` {r plot-subset-shapefile-unique-colors, fig.cap="Map of the footpaths in the study area where each feature is colored differently."}
217
218
ggplot() +
218
- geom_sf(data = footpath_HARV, aes(color = factor(OBJECTID)), size = 1.5) +
219
+ geom_sf(data = footpath_HARV, aes(color = factor(OBJECTID)), linewidth = 1.5) +
219
220
labs(color = 'Footpath ID') +
220
221
ggtitle("NEON Harvard Forest Field Site", subtitle = "Footpaths") +
221
222
coord_sf()
@@ -250,7 +251,7 @@ Now let's plot that data:
250
251
251
252
``` {r harv-boardwalk-map, fig.cap="Map of the boardwalks in the study area."}
252
253
ggplot() +
253
- geom_sf(data = boardwalk_HARV, size = 1.5) +
254
+ geom_sf(data = boardwalk_HARV, linewidth = 1.5) +
254
255
ggtitle("NEON Harvard Forest Field Site", subtitle = "Boardwalks") +
255
256
coord_sf()
256
257
```
@@ -283,7 +284,7 @@ Now we can plot the data:
283
284
284
285
``` {r harv-stone-wall-map, fig.cap="Map of the stone walls in the study area where each feature is colored differently."}
285
286
ggplot() +
286
- geom_sf(data = stoneWall_HARV, aes(color = factor(OBJECTID)), size = 1.5) +
287
+ geom_sf(data = stoneWall_HARV, aes(color = factor(OBJECTID)), linewidth = 1.5) +
287
288
labs(color = 'Wall ID') +
288
289
ggtitle("NEON Harvard Forest Field Site", subtitle = "Stonewalls") +
289
290
coord_sf()
@@ -389,7 +390,7 @@ Now we can create our plot.
389
390
390
391
``` {r harv-path-line-types, fig.cap="Roads and trails in the area with different line thickness for each type of paths."}
391
392
ggplot() +
392
- geom_sf(data = lines_HARV, aes(size = TYPE)) +
393
+ geom_sf(data = lines_HARV, aes(linewidth = TYPE)) +
393
394
scale_size_manual(values = line_width) +
394
395
ggtitle("NEON Harvard Forest Field Site",
395
396
subtitle = "Roads & Trails - Line width varies") +
@@ -405,21 +406,17 @@ ggplot() +
405
406
We can add a legend to our plot too. When we add a legend, we use the following
406
407
elements to specify labels and colors:
407
408
408
- - ` bottomright ` : We specify the location of our legend by using a default
409
- keyword. We could also use ` top ` , ` topright ` , etc.
410
- - ` levels(objectName$attributeName) ` : Label the legend elements using the
411
- categories of levels in an attribute (e.g., levels(lines\_ HARV$TYPE) means
412
- use the levels boardwalk, footpath, etc).
413
- - ` fill = ` : apply unique colors to the boxes in our legend. ` palette() ` is the
414
- default set of colors that R applies to all plots.
415
409
416
410
Let's add a legend to our plot. We will use the ` road_colors ` object
417
411
that we created above to color the legend. We can customize the
418
412
appearance of our legend by manually setting different parameters.
419
413
414
+
415
+
416
+
420
417
``` {r add-legend-to-plot, fig.cap="Roads and trails in the study area using thicker lines than the previous figure."}
421
418
ggplot() +
422
- geom_sf(data = lines_HARV, aes(color = TYPE), size = 1.5) +
419
+ geom_sf(data = lines_HARV, aes(color = TYPE), linewidth = 1.5) +
423
420
scale_color_manual(values = road_colors) +
424
421
labs(color = 'Road Type') +
425
422
ggtitle("NEON Harvard Forest Field Site",
@@ -435,7 +432,7 @@ parameters.
435
432
436
433
``` {r modify-legend-plot, fig.cap="Map of the paths in the study area with large-font and border around the legend."}
437
434
ggplot() +
438
- geom_sf(data = lines_HARV, aes(color = TYPE), size = 1.5) +
435
+ geom_sf(data = lines_HARV, aes(color = TYPE), linewidth = 1.5) +
439
436
scale_color_manual(values = road_colors) +
440
437
labs(color = 'Road Type') +
441
438
theme(legend.text = element_text(size = 20),
@@ -449,7 +446,7 @@ ggplot() +
449
446
new_colors <- c("springgreen", "blue", "magenta", "orange")
450
447
451
448
ggplot() +
452
- geom_sf(data = lines_HARV, aes(color = TYPE), size = 1.5) +
449
+ geom_sf(data = lines_HARV, aes(color = TYPE), linewidth = 1.5) +
453
450
scale_color_manual(values = new_colors) +
454
451
labs(color = 'Road Type') +
455
452
theme(legend.text = element_text(size = 20),
@@ -516,7 +513,7 @@ line width.
516
513
``` {r harv-paths-bike-horses, fig.cap="Roads and trails in the area highlighting paths where horses and bikes are allowed."}
517
514
ggplot() +
518
515
geom_sf(data = lines_HARV) +
519
- geom_sf(data = lines_showHarv, aes(color = BicyclesHo), size = 2) +
516
+ geom_sf(data = lines_showHarv, aes(color = BicyclesHo), linewidth = 2) +
520
517
scale_color_manual(values = "magenta") +
521
518
ggtitle("NEON Harvard Forest Field Site",
522
519
subtitle = "Roads Where Bikes and Horses Are Allowed") +
@@ -562,7 +559,7 @@ Now we can create our plot:
562
559
563
560
``` {r colored-state-boundaries, fig.cap="Map of the continental United States where the state lines are colored by region."}
564
561
ggplot() +
565
- geom_sf(data = state_boundary_US, aes(color = region), size = 1) +
562
+ geom_sf(data = state_boundary_US, aes(color = region), linewidth = 1) +
566
563
scale_color_manual(values = colors) +
567
564
ggtitle("Contiguous U.S. State Boundaries") +
568
565
coord_sf()
0 commit comments