Skip to content

Commit 5f66793

Browse files
authored
Merge pull request #414 from albhasan/plot_lines_by_attribute_challenge
Fixes #374. Thanks to @sstevens2
2 parents 23f9795 + 143c0fc commit 5f66793

File tree

1 file changed

+17
-20
lines changed

1 file changed

+17
-20
lines changed

episodes/07-vector-shapefile-attributes-in-r.Rmd

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@ library(sf)
3232

3333
```{r load-data, echo=FALSE, results="hide"}
3434
# learners will have this data loaded from previous episodes
35-
point_HARV <- st_read("data/NEON-DS-Site-Layout-Files/HARV/HARVtower_UTM18N.shp")
35+
point_HARV <-
36+
st_read("data/NEON-DS-Site-Layout-Files/HARV/HARVtower_UTM18N.shp")
3637
lines_HARV <- st_read("data/NEON-DS-Site-Layout-Files/HARV/HARV_roads.shp")
37-
aoi_boundary_HARV <- st_read(
38-
"data/NEON-DS-Site-Layout-Files/HARV/HarClip_UTMZ18.shp")
38+
aoi_boundary_HARV <-
39+
st_read("data/NEON-DS-Site-Layout-Files/HARV/HarClip_UTMZ18.shp")
3940
```
4041

4142
:::::::::::::::::::::::::::::::::::::::::: prereq
@@ -490,33 +491,29 @@ other lines can be black.
490491

491492
## Answers
492493

493-
First we need to make sure that the `BicyclesHo` attribute is a factor and
494-
check how many levels it has.
494+
First we explore the `BicyclesHo` attribute to learn the values that correspond
495+
to the roads we need.
495496

496497
```{r}
497-
lines_HARV$BicyclesHo <- as.factor(lines_HARV$BicyclesHo)
498-
class(lines_HARV$BicyclesHo)
499-
levels(lines_HARV$BicyclesHo)
498+
lines_HARV %>%
499+
pull(BicyclesHo) %>%
500+
unique()
500501
```
501502

502-
Next, we will create a new object `lines_removeNA` that removes missing values.
503+
Now, we can create a data frame with only those roads where bicycles and horses
504+
are allowed.
503505

504506
```{r}
505-
lines_removeNA <- lines_HARV[!is.na(lines_HARV$BicyclesHo),]
507+
lines_showHarv <-
508+
lines_HARV %>%
509+
filter(BicyclesHo == "Bicycles and Horses Allowed")
510+
506511
```
507512

508-
In our plot, we will set colors so that only the allowed roads are magenta, and
509-
we will set line width so that the first factor level is thicker than the
510-
others.
513+
Finally, we plot the needed roads after setting them to magenta and a thicker
514+
line width.
511515

512516
```{r harv-paths-bike-horses, fig.cap="Roads and trails in the area highlighting paths where horses and bikes are allowed."}
513-
# First, create a data frame with only those roads where bicycles and horses
514-
# are allowed
515-
lines_showHarv <-
516-
lines_removeNA %>%
517-
filter(BicyclesHo == "Bicycles and Horses Allowed")
518-
519-
# Next, visualise using ggplot
520517
ggplot() +
521518
geom_sf(data = lines_HARV) +
522519
geom_sf(data = lines_showHarv, aes(color = BicyclesHo), size = 2) +

0 commit comments

Comments
 (0)