Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Episode 7 updates for line sizes and other #463

Merged
merged 3 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 All @@ -34,7 +26,7 @@

This lesson assumes you have some knowledge of `R`. If you've never
used `R` before, or need a refresher, start with our
[Introduction to R for Geospatial Data](http://www.datacarpentry.org/r-intro-geospatial/)

Check warning on line 29 in index.md

View workflow job for this annotation

GitHub Actions / Build markdown source files if valid

[needs HTTPS]: [Introduction to R for Geospatial Data](http://www.datacarpentry.org/r-intro-geospatial/)
lesson.

### Geospatial Skill Level
Expand All @@ -42,20 +34,20 @@
This lesson assumes you have some knowledge of geospatial data types
and common file formats. If you have never worked with geospatial
data before, or need a refresher, start with our
[Introduction to Geospatial Concepts](http://www.datacarpentry.org/organization-geospatial/)

Check warning on line 37 in index.md

View workflow job for this annotation

GitHub Actions / Build markdown source files if valid

[needs HTTPS]: [Introduction to Geospatial Concepts](http://www.datacarpentry.org/organization-geospatial/)
lesson.

### Install Software and Download Data

For installation instructions and to download the data used in this
lesson, see the
[Geospatial Workshop Overview](http://www.datacarpentry.org/geospatial-workshop/#setup).

Check warning on line 44 in index.md

View workflow job for this annotation

GitHub Actions / Build markdown source files if valid

[needs HTTPS]: [Geospatial Workshop Overview](http://www.datacarpentry.org/geospatial-workshop/#setup)

### Setup RStudio Project

Make sure you have set up a RStudio project for this lesson, as
described in the
[setup instructions](http://www.datacarpentry.org/geospatial-workshop/#setup)

Check warning on line 50 in index.md

View workflow job for this annotation

GitHub Actions / Build markdown source files if valid

[needs HTTPS]: [setup instructions](http://www.datacarpentry.org/geospatial-workshop/#setup)
and that your working directory is correctly set.


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
Loading