Skip to content

Commit 675aba8

Browse files
authored
Merge pull request #385 from albhasan/m2022q1_e2
Lesson 2 updated by replacing calls to raster and rgdal packages.
2 parents 2cc2f7b + 39f7e55 commit 675aba8

File tree

1 file changed

+41
-29
lines changed

1 file changed

+41
-29
lines changed

episodes/02-raster-plot.Rmd

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,16 @@ source("setup.R")
2525
::::::::::::::::::::::::::::::::::::::::::::::::::
2626

2727
```{r load-libraries, echo=FALSE, results="hide", message=FALSE, warning=FALSE}
28-
library(raster)
29-
library(rgdal)
28+
library(terra)
3029
library(ggplot2)
3130
library(dplyr)
3231
```
3332

3433
```{r load-data, echo=FALSE}
3534
# Learners will have this data loaded from earlier episode
3635
# DSM data for Harvard Forest
37-
DSM_HARV <- raster("data/NEON-DS-Airborne-Remote-Sensing/HARV/DSM/HARV_dsmCrop.tif")
36+
DSM_HARV <-
37+
rast("data/NEON-DS-Airborne-Remote-Sensing/HARV/DSM/HARV_dsmCrop.tif")
3838
3939
DSM_HARV_df <- as.data.frame(DSM_HARV, xy = TRUE)
4040
```
@@ -53,16 +53,20 @@ data, and other prerequisites you will need to work through the examples in this
5353

5454
This episode covers how to plot a raster in R using the `ggplot2`
5555
package with customized coloring schemes.
56-
It also covers how to layer a raster on top of a hillshade to produce
57-
an eloquent map. We will continue working with the Digital Surface Model (DSM) raster
58-
for the NEON Harvard Forest Field Site.
56+
It also covers how to layer a raster on top of a hillshade to produce an
57+
eloquent map. We will continue working with the Digital Surface Model (DSM)
58+
raster for the NEON Harvard Forest Field Site.
5959

6060
## Plotting Data Using Breaks
6161

6262
In the previous episode, we viewed our data using a continuous color ramp. For
63-
clarity and visibility of the plot, we may prefer to view the data "symbolized" or colored according to ranges of values. This is comparable to a "classified"
64-
map. To do this, we need to tell `ggplot` how many groups to break our data into, and
65-
where those breaks should be. To make these decisions, it is useful to first explore the distribution of the data using a bar plot. To begin with, we will use `dplyr`'s `mutate()` function combined with `cut()` to split the data into 3 bins.
63+
clarity and visibility of the plot, we may prefer to view the data "symbolized"
64+
or colored according to ranges of values. This is comparable to a "classified"
65+
map. To do this, we need to tell `ggplot` how many groups to break our data
66+
into, and where those breaks should be. To make these decisions, it is useful
67+
to first explore the distribution of the data using a bar plot. To begin with,
68+
we will use `dplyr`'s `mutate()` function combined with `cut()` to split the
69+
data into 3 bins.
6670

6771
```{r histogram-breaks-ggplot}
6872
@@ -74,15 +78,15 @@ ggplot() +
7478
7579
```
7680

77-
If we want to know the cutoff values for the groups, we can ask for the unique values
78-
of `fct_elevation`:
81+
If we want to know the cutoff values for the groups, we can ask for the unique
82+
values of `fct_elevation`:
7983

8084
```{r unique-breaks}
8185
unique(DSM_HARV_df$fct_elevation)
8286
```
8387

84-
And we can get the count of values in each group using `dplyr`'s
85-
`group_by()` and `count()` functions:
88+
And we can get the count of values in each group using `dplyr`'s `group_by()`
89+
and `count()` functions:
8690

8791
```{r breaks-count}
8892
DSM_HARV_df %>%
@@ -93,8 +97,8 @@ DSM_HARV_df %>%
9397
We might prefer to customize the cutoff values for these groups.
9498
Lets round the cutoff values so that we have groups for the ranges of
9599
301–350 m, 351–400 m, and 401–450 m.
96-
To implement this we will give `mutate()` a numeric vector of break points instead
97-
of the number of breaks we want.
100+
To implement this we will give `mutate()` a numeric vector of break points
101+
instead of the number of breaks we want.
98102

99103
```{r custom-bins}
100104
custom_bins <- c(300, 350, 400, 450)
@@ -109,9 +113,11 @@ unique(DSM_HARV_df$fct_elevation_2)
109113

110114
## Data Tips
111115

112-
Note that when we assign break values a set of 4 values will result in 3 bins of data.
116+
Note that when we assign break values a set of 4 values will result in 3 bins
117+
of data.
113118

114-
The bin intervals are shown using `(` to mean exclusive and `]` to mean inclusive. For example: `(305, 342]` means "from 306 through 342".
119+
The bin intervals are shown using `(` to mean exclusive and `]` to mean
120+
inclusive. For example: `(305, 342]` means "from 306 through 342".
115121

116122

117123
::::::::::::::::::::::::::::::::::::::::::::::::::
@@ -131,7 +137,8 @@ DSM_HARV_df %>%
131137
count()
132138
```
133139

134-
We can use those groups to plot our raster data, with each group being a different color:
140+
We can use those groups to plot our raster data, with each group being a
141+
different color:
135142

136143
```{r raster-with-breaks}
137144
ggplot() +
@@ -167,8 +174,8 @@ ggplot() +
167174

168175
If we need to create multiple plots using the same color palette, we can create
169176
an R object (`my_col`) for the set of colors that we want to use. We can then
170-
quickly change the palette across all plots by modifying the `my_col`
171-
object, rather than each individual plot.
177+
quickly change the palette across all plots by modifying the `my_col` object,
178+
rather than each individual plot.
172179

173180
We can label the x- and y-axes of our plot too using `xlab` and `ylab`.
174181
We can also give the legend a more meaningful title by passing a value
@@ -203,7 +210,8 @@ ggplot() +
203210

204211
Create a plot of the Harvard Forest Digital Surface Model (DSM) that has:
205212

206-
1. Six classified ranges of values (break points) that are evenly divided among the range of pixel values.
213+
1. Six classified ranges of values (break points) that are evenly divided among
214+
the range of pixel values.
207215
2. Axis labels.
208216
3. A plot title.
209217

@@ -244,7 +252,7 @@ First we need to read in our DSM hillshade data and view the structure:
244252

245253
```{r}
246254
DSM_hill_HARV <-
247-
raster("data/NEON-DS-Airborne-Remote-Sensing/HARV/DSM/HARV_DSMhill.tif")
255+
rast("data/NEON-DS-Airborne-Remote-Sensing/HARV/DSM/HARV_DSMhill.tif")
248256
249257
DSM_hill_HARV
250258
```
@@ -302,9 +310,9 @@ ggplot() +
302310

303311
## Challenge: Create DTM \& DSM for SJER
304312

305-
Use the files in the `data/NEON-DS-Airborne-Remote-Sensing/SJER/` directory to create a Digital
306-
Terrain Model map and Digital Surface Model map of the San Joaquin Experimental
307-
Range field site.
313+
Use the files in the `data/NEON-DS-Airborne-Remote-Sensing/SJER/` directory to
314+
create a Digital Terrain Model map and Digital Surface Model map of the San
315+
Joaquin Experimental Range field site.
308316

309317
Make sure to:
310318

@@ -322,12 +330,14 @@ Make sure to:
322330
# CREATE DSM MAPS
323331
324332
# import DSM data
325-
DSM_SJER <- raster("data/NEON-DS-Airborne-Remote-Sensing/SJER/DSM/SJER_dsmCrop.tif")
333+
DSM_SJER <-
334+
rast("data/NEON-DS-Airborne-Remote-Sensing/SJER/DSM/SJER_dsmCrop.tif")
326335
# convert to a df for plotting
327336
DSM_SJER_df <- as.data.frame(DSM_SJER, xy = TRUE)
328337
329338
# import DSM hillshade
330-
DSM_hill_SJER <- raster("data/NEON-DS-Airborne-Remote-Sensing/SJER/DSM/SJER_dsmHill.tif")
339+
DSM_hill_SJER <-
340+
rast("data/NEON-DS-Airborne-Remote-Sensing/SJER/DSM/SJER_dsmHill.tif")
331341
# convert to a df for plotting
332342
DSM_hill_SJER_df <- as.data.frame(DSM_hill_SJER, xy = TRUE)
333343
@@ -356,11 +366,13 @@ ggplot() +
356366
357367
# CREATE DTM MAP
358368
# import DTM
359-
DTM_SJER <- raster("data/NEON-DS-Airborne-Remote-Sensing/SJER/DTM/SJER_dtmCrop.tif")
369+
DTM_SJER <-
370+
rast("data/NEON-DS-Airborne-Remote-Sensing/SJER/DTM/SJER_dtmCrop.tif")
360371
DTM_SJER_df <- as.data.frame(DTM_SJER, xy = TRUE)
361372
362373
# DTM Hillshade
363-
DTM_hill_SJER <- raster("data/NEON-DS-Airborne-Remote-Sensing/SJER/DTM/SJER_dtmHill.tif")
374+
DTM_hill_SJER <-
375+
rast("data/NEON-DS-Airborne-Remote-Sensing/SJER/DTM/SJER_dtmHill.tif")
364376
DTM_hill_SJER_df <- as.data.frame(DTM_hill_SJER, xy = TRUE)
365377
366378
ggplot() +

0 commit comments

Comments
 (0)