Skip to content

Commit

Permalink
Merge pull request #463 from jonjab/main
Browse files Browse the repository at this point in the history
Episode 7 updates for line sizes and other
  • Loading branch information
jonjab authored Jan 21, 2025
2 parents 108a5c2 + af4dd6e commit 19d908b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 28 deletions.
33 changes: 15 additions & 18 deletions episodes/07-vector-shapefile-attributes-in-r.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ and other prerequisites you will need to work through the examples in this
episode.



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

This episode continues our discussion of vector layer attributes and covers how
Expand All @@ -58,7 +59,7 @@ attribute values.

## Load the Data

We will continue using the `sf`, `terra` and `ggplot2` packages in this
We will continue using the `sf`, `terra` `dplyr` and `ggplot2` packages in this
episode. Make sure that you have these packages loaded. We will continue to
work with the three ESRI `shapefiles` (vector layers) that we loaded in the
[Open and Plot Vector Layers in R](06-vector-open-shapefile-in-r/) episode.
Expand Down Expand Up @@ -86,7 +87,7 @@ point_HARV

We can use the `ncol` function to count the number of attributes associated
with a spatial object too. Note that the geometry is just another column and
counts towards the total.
counts towards the total. Let's look at the roads file:

```{r shapefile-attributes}
ncol(lines_HARV)
Expand Down Expand Up @@ -215,7 +216,7 @@ connecting line thickness to a data variable.

```{r plot-subset-shapefile-unique-colors, fig.cap="Map of the footpaths in the study area where each feature is colored differently."}
ggplot() +
geom_sf(data = footpath_HARV, aes(color = factor(OBJECTID)), size = 1.5) +
geom_sf(data = footpath_HARV, aes(color = factor(OBJECTID)), linewidth = 1.5) +
labs(color = 'Footpath ID') +
ggtitle("NEON Harvard Forest Field Site", subtitle = "Footpaths") +
coord_sf()
Expand Down Expand Up @@ -250,7 +251,7 @@ Now let's plot that data:

```{r harv-boardwalk-map, fig.cap="Map of the boardwalks in the study area."}
ggplot() +
geom_sf(data = boardwalk_HARV, size = 1.5) +
geom_sf(data = boardwalk_HARV, linewidth = 1.5) +
ggtitle("NEON Harvard Forest Field Site", subtitle = "Boardwalks") +
coord_sf()
```
Expand Down Expand Up @@ -283,7 +284,7 @@ Now we can plot the data:

```{r harv-stone-wall-map, fig.cap="Map of the stone walls in the study area where each feature is colored differently."}
ggplot() +
geom_sf(data = stoneWall_HARV, aes(color = factor(OBJECTID)), size = 1.5) +
geom_sf(data = stoneWall_HARV, aes(color = factor(OBJECTID)), linewidth = 1.5) +
labs(color = 'Wall ID') +
ggtitle("NEON Harvard Forest Field Site", subtitle = "Stonewalls") +
coord_sf()
Expand Down Expand Up @@ -389,7 +390,7 @@ Now we can create our plot.

```{r harv-path-line-types, fig.cap="Roads and trails in the area with different line thickness for each type of paths."}
ggplot() +
geom_sf(data = lines_HARV, aes(size = TYPE)) +
geom_sf(data = lines_HARV, aes(linewidth = TYPE)) +
scale_size_manual(values = line_width) +
ggtitle("NEON Harvard Forest Field Site",
subtitle = "Roads & Trails - Line width varies") +
Expand All @@ -405,21 +406,17 @@ ggplot() +
We can add a legend to our plot too. When we add a legend, we use the following
elements to specify labels and colors:

- `bottomright`: We specify the location of our legend by using a default
keyword. We could also use `top`, `topright`, etc.
- `levels(objectName$attributeName)`: Label the legend elements using the
categories of levels in an attribute (e.g., levels(lines\_HARV$TYPE) means
use the levels boardwalk, footpath, etc).
- `fill =`: apply unique colors to the boxes in our legend. `palette()` is the
default set of colors that R applies to all plots.

Let's add a legend to our plot. We will use the `road_colors` object
that we created above to color the legend. We can customize the
appearance of our legend by manually setting different parameters.




```{r add-legend-to-plot, fig.cap="Roads and trails in the study area using thicker lines than the previous figure."}
ggplot() +
geom_sf(data = lines_HARV, aes(color = TYPE), size = 1.5) +
geom_sf(data = lines_HARV, aes(color = TYPE), linewidth = 1.5) +
scale_color_manual(values = road_colors) +
labs(color = 'Road Type') +
ggtitle("NEON Harvard Forest Field Site",
Expand All @@ -435,7 +432,7 @@ parameters.

```{r modify-legend-plot, fig.cap="Map of the paths in the study area with large-font and border around the legend."}
ggplot() +
geom_sf(data = lines_HARV, aes(color = TYPE), size = 1.5) +
geom_sf(data = lines_HARV, aes(color = TYPE), linewidth = 1.5) +
scale_color_manual(values = road_colors) +
labs(color = 'Road Type') +
theme(legend.text = element_text(size = 20),
Expand All @@ -449,7 +446,7 @@ ggplot() +
new_colors <- c("springgreen", "blue", "magenta", "orange")
ggplot() +
geom_sf(data = lines_HARV, aes(color = TYPE), size = 1.5) +
geom_sf(data = lines_HARV, aes(color = TYPE), linewidth = 1.5) +
scale_color_manual(values = new_colors) +
labs(color = 'Road Type') +
theme(legend.text = element_text(size = 20),
Expand Down Expand Up @@ -516,7 +513,7 @@ line width.
```{r harv-paths-bike-horses, fig.cap="Roads and trails in the area highlighting paths where horses and bikes are allowed."}
ggplot() +
geom_sf(data = lines_HARV) +
geom_sf(data = lines_showHarv, aes(color = BicyclesHo), size = 2) +
geom_sf(data = lines_showHarv, aes(color = BicyclesHo), linewidth = 2) +
scale_color_manual(values = "magenta") +
ggtitle("NEON Harvard Forest Field Site",
subtitle = "Roads Where Bikes and Horses Are Allowed") +
Expand Down Expand Up @@ -562,7 +559,7 @@ Now we can create our plot:

```{r colored-state-boundaries, fig.cap="Map of the continental United States where the state lines are colored by region."}
ggplot() +
geom_sf(data = state_boundary_US, aes(color = region), size = 1) +
geom_sf(data = state_boundary_US, aes(color = region), linewidth = 1) +
scale_color_manual(values = colors) +
ggtitle("Contiguous U.S. State Boundaries") +
coord_sf()
Expand Down
12 changes: 2 additions & 10 deletions index.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
---
maintainers:
- Leah Wasser
- Jemma Stachelek
- Tyson Swetnam
- Lauren O'Brien
- Janani Selvaraj
- Lachlan Deer
- Chris Prener
- Juan Fung

site: sandpaper::sandpaper_site
---

**Lesson Authors:** Leah A. Wasser, Megan A. Jones, Zack Brym, Kristina Riemer, Jason Williams, Jeff Hollister, Mike Smorul, Jemma Stachelek

**Lesson Maintainers:** {{ page.maintainers | join: ', ' }}


The episodes in this lesson cover how to open, work with, and plot
vector and raster-format spatial data in R. Additional topics include
Expand Down
16 changes: 16 additions & 0 deletions r-raster-vector-geospatial.Rproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Version: 1.0
ProjectId: 32618674-7875-479d-aff6-55bf7903a906

RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: Sweave
LaTeX: pdfLaTeX

BuildType: Website

0 comments on commit 19d908b

Please sign in to comment.