You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -52,12 +51,13 @@ rasters in R, including CRS and resolution. We will also explore missing and bad
52
51
data values as stored in a raster and how R handles these elements.
53
52
54
53
We will continue to work with the `dplyr` and `ggplot2` packages that were introduced
55
-
in the [Introduction to R for Geospatial Data](https://datacarpentry.org/r-intro-geospatial/) lesson. We will use two additional packages in this episode to work with raster data - the
56
-
`raster` and `rgdal` packages. Make sure that you have these packages loaded.
54
+
in the [Introduction to R for Geospatial Data](https://datacarpentry.org/r-intro-geospatial/)
55
+
lesson. We will use two additional packages in this episode to work with raster
56
+
data - the `terra` and `sf` packages. Make sure that you have these packages
The `raster` package has an built-in function for conversion to a plotable dataframe.
151
+
The `terra` package has an built-in function for conversion to a plotable dataframe.
155
152
156
153
```{r}
157
154
DSM_HARV_df <- as.data.frame(DSM_HARV, xy = TRUE)
@@ -164,8 +161,12 @@ dataframe format.
164
161
str(DSM_HARV_df)
165
162
```
166
163
167
-
We can use `ggplot()` to plot this data. We will set the color scale to `scale_fill_viridis_c`
168
-
which is a color-blindness friendly color scale. We will also use the `coord_quickmap()` function to use an approximate Mercator projection for our plots. This approximation is suitable for small areas that are not too close to the poles. Other coordinate systems are available in ggplot2 if needed, you can learn about them at their help page `?coord_map`.
164
+
We can use `ggplot()` to plot this data. We will set the color scale to
165
+
`scale_fill_viridis_c` which is a color-blindness friendly color scale. We will
166
+
also use the `coord_quickmap()` function to use an approximate Mercator
167
+
projection for our plots. This approximation is suitable for small areas that
168
+
are not too close to the poles. Other coordinate systems are available in
169
+
ggplot2 if needed, you can learn about them at their help page `?coord_map`.
169
170
170
171
```{r ggplot-raster, fig.cap="Raster plot with ggplot2 using the viridis color scale"}
171
172
@@ -189,7 +190,7 @@ More information about the Viridis palette used above at
189
190
190
191
## Plotting Tip
191
192
192
-
For faster, simpler plots, you can use the `plot` function from the `raster` package.
193
+
For faster, simpler plots, you can use the `plot` function from the `terra` package.
193
194
194
195
::::::::::::::: solution
195
196
@@ -222,7 +223,7 @@ We can view the CRS string associated with our R object using the`crs()`
222
223
function.
223
224
224
225
```{r view-resolution-units}
225
-
crs(DSM_HARV)
226
+
crs(DSM_HARV, proj = TRUE)
226
227
```
227
228
228
229
::::::::::::::::::::::::::::::::::::::: challenge
@@ -254,7 +255,8 @@ and datum (`datum=`).
254
255
255
256
### UTM Proj4 String
256
257
257
-
Our projection string for `DSM_HARV` specifies the UTM projection as follows:
258
+
A projection string (like the one of `DSM_HARV`) specifies the UTM projection
@@ -269,7 +271,8 @@ Our projection string for `DSM_HARV` specifies the UTM projection as follows:
269
271
Note that the zone is unique to the UTM projection. Not all CRSs will have a
270
272
zone. Image source: Chrismurf at English Wikipedia, via [Wikimedia Commons](https://en.wikipedia.org/wiki/Universal_Transverse_Mercator_coordinate_system#/media/File:Utm-zones-USA.svg) (CC-BY).
271
273
272
-

274
+
275
+
{alt='UTM zones in the USA.'}
273
276
274
277
## Calculate Raster Min and Max Values
275
278
@@ -281,9 +284,11 @@ Raster statistics are often calculated and embedded in a GeoTIFF for us. We
# aggregate cells from 0.25m to 2m for plotting to speed up the lesson and
349
354
# save memory
@@ -389,19 +394,25 @@ ggplot() +
389
394
390
395
```
391
396
392
-
If your raster already has `NA` values set correctly but you aren't sure where they are, you can deliberately plot them in a particular colour. This can be useful when checking a dataset's coverage. For instance, sometimes data can be missing where a sensor could not 'see' its target data, and you may wish to locate that missing data and fill it in.
397
+
If your raster already has `NA` values set correctly but you aren't sure where
398
+
they are, you can deliberately plot them in a particular colour. This can be
399
+
useful when checking a dataset's coverage. For instance, sometimes data can be
400
+
missing where a sensor could not 'see' its target data, and you may wish to
401
+
locate that missing data and fill it in.
393
402
394
-
To highlight `NA` values in ggplot, alter the `scale_fill_*()` layer to contain a colour instruction for `NA` values, like `scale_fill_viridis_c(na.value = 'deeppink')`
403
+
To highlight `NA` values in ggplot, alter the `scale_fill_*()` layer to contain
404
+
a colour instruction for `NA` values, like `scale_fill_viridis_c(na.value = 'deeppink')`
395
405
396
406
```{r, echo=FALSE}
397
407
# demonstration code
398
408
# function to replace 0 with NA where all three values are 0 only
0 commit comments