Skip to content

Commit c5d6da3

Browse files
authored
Merge pull request #276 from Alice-MacQueen/alice-macqueen
Cleaning up some RGB value manipulation in Episode 12 Closes #227
2 parents 0402651 + 8edb4fd commit c5d6da3

File tree

1 file changed

+57
-42
lines changed

1 file changed

+57
-42
lines changed

_episodes_rmd/12-time-series-raster.Rmd

Lines changed: 57 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ library(rgdal)
2929
library(ggplot2)
3030
library(dplyr)
3131
library(scales)
32-
library(reshape)
32+
library(tidyr)
3333
```
3434

3535

@@ -68,13 +68,14 @@ available from Landsat data. The RGB directory contains RGB images for each time
6868
period that NDVI is available.
6969

7070
### Getting Started
71-
In this episode, we will use the `raster`, `rgdal`, `reshape`, and `scales` packages. Make sure you have them loaded.
71+
In this episode, we will use the `raster`, `rgdal`, `scales`, `tidyr`, and `ggplot2` packages. Make sure you have them loaded.
7272

7373
```{r, eval = FALSE}
7474
library(raster)
7575
library(rgdal)
76-
library(reshape)
7776
library(scales)
77+
library(tidyr)
78+
library(ggplot2)
7879
```
7980

8081
To begin, we will create a list of raster files using the `list.files()`
@@ -158,14 +159,12 @@ UTM Zone 19.
158159
## Plotting Time Series Data
159160
Once we have created our RasterStack, we can visualize our data. We can use
160161
the `ggplot()` command to create a multi-panelled plot showing each band in our RasterStack. First we
161-
need to create a data frame object. Because there are multiple bands in our data, we will reshape (or "melt")
162-
the data so that we have a single column with
163-
the NDVI observations. We will use the function
164-
`melt()` from the `reshape` package to do this:
162+
need to create a data frame object. Because there are multiple columns in our data that are not variables, we will tidy (or "gather") the data so that we have a single column with the NDVI observations.
163+
We will use the function `gather()` from the `tidyr` package to do this:
165164
166165
```{r plot-time-series}
167166
NDVI_HARV_stack_df <- as.data.frame(NDVI_HARV_stack, xy = TRUE) %>%
168-
melt(id.vars = c('x','y'))
167+
gather(variable, value, -(x:y))
169168
```
170169
171170
Now we can plot our data using `ggplot()`. We want
@@ -199,7 +198,7 @@ After applying our scale factor, we can recreate our plot using the same code we
199198

200199
```{r ndvi-stack-wrap}
201200
NDVI_HARV_stack_df <- as.data.frame(NDVI_HARV_stack, xy = TRUE) %>%
202-
melt(id.vars = c('x','y'))
201+
gather(variable, value, -(x:y))
203202
204203
ggplot() +
205204
geom_raster(data = NDVI_HARV_stack_df , aes(x = x, y = y, fill = value)) +
@@ -270,9 +269,8 @@ har_met_daily$date <- as.Date(har_met_daily$date, format = "%Y-%m-%d")
270269
We only want to look at the data from 2011:
271270

272271
```{r}
273-
yr_11_daily_avg <- subset(har_met_daily,
274-
date >= as.Date('2011-01-01') &
275-
date <= as.Date('2011-12-31'))
272+
yr_11_daily_avg <- har_met_daily %>%
273+
filter(between(date, as.Date('2011-01-01'), as.Date('2011-12-31')))
276274
```
277275

278276
Now we can plot the air temperature (the `airt` column) by Julian day (the `jd` column):
@@ -295,42 +293,59 @@ derive our NDVI rasters to try to understand what appear to be outlier NDVI valu
295293
# code not shown, demonstration only
296294
# Plot RGB data for Julian day 133
297295
RGB_133 <- stack("data/NEON-DS-Landsat-NDVI/HARV/2011/RGB/133_HARV_landRGB.tif")
298-
RGB_133_df <- as.data.frame(RGB_133, xy = TRUE)
299296
quantiles = c(0.02, 0.98)
300-
r <- quantile(RGB_133_df$X133_HARV_landRGB.1, quantiles, na.rm = TRUE)
301-
g <- quantile(RGB_133_df$X133_HARV_landRGB.2, quantiles, na.rm = TRUE)
302-
b <- quantile(RGB_133_df$X133_HARV_landRGB.3, quantiles, na.rm = TRUE)
303-
tempR <- (RGB_133_df$X133_HARV_landRGB.1 - r[1])/(r[2] - r[1])
304-
tempG <- (RGB_133_df$X133_HARV_landRGB.2 - g[1])/(g[2] - g[1])
305-
tempB <- (RGB_133_df$X133_HARV_landRGB.3 - b[1])/(b[2] - b[1])
306-
tempR[tempR < 0] <- 0
307-
tempG[tempG < 0] <- 0
308-
tempB[tempB < 0] <- 0
309-
tempR[tempR > 1] <- 1
310-
tempG[tempG > 1] <- 1
311-
tempB[tempB > 1] <- 1
312-
RGB_133_df$rgb <- rgb(tempR,tempG,tempB)
297+
r <- quantile(RGB_133$X133_HARV_landRGB_1, quantiles, na.rm = TRUE)
298+
g <- quantile(RGB_133$X133_HARV_landRGB_2, quantiles, na.rm = TRUE)
299+
b <- quantile(RGB_133$X133_HARV_landRGB_3, quantiles, na.rm = TRUE)
300+
RGB_133_df <- as.data.frame(RGB_133, xy = TRUE) %>%
301+
mutate(tempR = (X133_HARV_landRGB_1 - r[1])/(r[2] - r[1]),
302+
tempG = (X133_HARV_landRGB_2 - g[1])/(g[2] - g[1]),
303+
tempB = (X133_HARV_landRGB_3 - b[1])/(b[2] - b[1]),
304+
tempR = case_when(
305+
tempR < 0 ~ 0,
306+
tempR > 1 ~ 1,
307+
TRUE ~ tempR),
308+
tempG = case_when(
309+
tempG < 0 ~ 0,
310+
tempG > 1 ~ 1,
311+
TRUE ~ tempG),
312+
tempB = case_when(
313+
tempB < 0 ~ 0,
314+
tempB > 1 ~ 1,
315+
TRUE ~ tempB),
316+
rgb = rgb(tempR,tempG,tempB)
317+
) %>%
318+
dplyr::select(-(tempR:tempB))
319+
313320
ggplot() +
314321
geom_raster(data = RGB_133_df, aes(x, y), fill = RGB_133_df$rgb) +
315322
ggtitle("Julian day 133")
316323
317324
# Plot RGB data for Julian day 197
318325
RGB_197 <- stack("data/NEON-DS-Landsat-NDVI/HARV/2011/RGB/197_HARV_landRGB.tif")
319326
RGB_197 <- RGB_197/255
320-
RGB_197_df <- as.data.frame(RGB_197, xy = TRUE)
321-
r <- quantile(RGB_197_df$X197_HARV_landRGB.1, quantiles, na.rm = TRUE)
322-
g <- quantile(RGB_197_df$X197_HARV_landRGB.2, quantiles, na.rm = TRUE)
323-
b <- quantile(RGB_197_df$X197_HARV_landRGB.3, quantiles, na.rm = TRUE)
324-
tempR <- (RGB_197_df$X197_HARV_landRGB.1 - r[1])/(r[2] - r[1])
325-
tempG <- (RGB_197_df$X197_HARV_landRGB.2 - g[1])/(g[2] - g[1])
326-
tempB <- (RGB_197_df$X197_HARV_landRGB.3 - b[1])/(b[2] - b[1])
327-
tempR[tempR < 0] <- 0
328-
tempG[tempG < 0] <- 0
329-
tempB[tempB < 0] <- 0
330-
tempR[tempR > 1] <- 1
331-
tempG[tempG > 1] <- 1
332-
tempB[tempB > 1] <- 1
333-
RGB_197_df$rgb <- rgb(tempR,tempG,tempB)
327+
r <- quantile(RGB_197$X197_HARV_landRGB_1, quantiles, na.rm = TRUE)
328+
g <- quantile(RGB_197$X197_HARV_landRGB_2, quantiles, na.rm = TRUE)
329+
b <- quantile(RGB_197$X197_HARV_landRGB_3, quantiles, na.rm = TRUE)
330+
RGB_197_df <- as.data.frame(RGB_197, xy = TRUE) %>%
331+
mutate(tempR = (X197_HARV_landRGB_1 - r[1])/(r[2] - r[1]),
332+
tempR = case_when(
333+
tempR < 0 ~ 0,
334+
tempR > 1 ~ 1,
335+
TRUE ~ tempR),
336+
tempG = (X197_HARV_landRGB_2 - g[1])/(g[2] - g[1]),
337+
tempG = case_when(
338+
tempG < 0 ~ 0,
339+
tempG > 1 ~ 1,
340+
TRUE ~ tempG),
341+
tempB = (X197_HARV_landRGB_3 - b[1])/(b[2] - b[1]),
342+
tempB = case_when(
343+
tempB < 0 ~ 0,
344+
tempB > 1 ~ 1,
345+
TRUE ~ tempB),
346+
rgb = rgb(tempR,tempG,tempB)
347+
) %>%
348+
dplyr::select(-(tempR:tempB)) # get rid of the temporary variables we created.
334349
ggplot() +
335350
geom_raster(data = RGB_197_df, aes(x, y), fill = RGB_197_df$rgb) +
336351
ggtitle("Julian day 197")
@@ -366,7 +381,7 @@ ggplot() +
366381
> > We create RGB colors from the three channels:
367382
> >
368383
> > ```{r}
369-
> > RGB_277_df$rgb <- with(RGB_277_df, rgb(X277_HARV_landRGB.1, X277_HARV_landRGB.2, X277_HARV_landRGB.3,1))
384+
> > RGB_277_df$rgb <- with(RGB_277_df, rgb(X277_HARV_landRGB_1, X277_HARV_landRGB_2, X277_HARV_landRGB_3,1))
370385
> > ```
371386
> >
372387
> > Finally, we can plot the RGB data for Julian day 277.
@@ -383,7 +398,7 @@ ggplot() +
383398
> > RGB_293 <- stack("data/NEON-DS-Landsat-NDVI/HARV/2011/RGB/293_HARV_landRGB.tif")
384399
> > RGB_293 <- RGB_293/255
385400
> > RGB_293_df <- as.data.frame(RGB_293, xy = TRUE)
386-
> > RGB_293_df$rgb <- with(RGB_293_df, rgb(X293_HARV_landRGB.1, X293_HARV_landRGB.2, X293_HARV_landRGB.3,1))
401+
> > RGB_293_df$rgb <- with(RGB_293_df, rgb(X293_HARV_landRGB_1, X293_HARV_landRGB_2, X293_HARV_landRGB_3,1))
387402
> > ggplot() +
388403
> > geom_raster(data = RGB_293_df, aes(x, y), fill = RGB_293_df$rgb) +
389404
> > ggtitle("Julian day 293")

0 commit comments

Comments
 (0)