r-raster-vector-geospatial
r-raster-vector-geospatial copied to clipboard
Cleaning up some RGB value manipulation in episode 12
In episode _episodes_rmd/12-time-series-raster.Rmd
There is a big chunk of code that can probably be made to look nicer via dplyr:
# Plot RGB data for Julian day 133
RGB_133 <- stack("data/NEON-DS-Landsat-NDVI/HARV/2011/RGB/133_HARV_landRGB.tif")
RGB_133_df <- raster::as.data.frame(RGB_133, xy = TRUE)
quantiles = c(0.02, 0.98)
r <- quantile(RGB_133_df$X133_HARV_landRGB.1, quantiles, na.rm = TRUE)
g <- quantile(RGB_133_df$X133_HARV_landRGB.2, quantiles, na.rm = TRUE)
b <- quantile(RGB_133_df$X133_HARV_landRGB.3, quantiles, na.rm = TRUE)
tempR <- (RGB_133_df$X133_HARV_landRGB.1 - r[1])/(r[2] - r[1])
tempG <- (RGB_133_df$X133_HARV_landRGB.2 - g[1])/(g[2] - g[1])
tempB <- (RGB_133_df$X133_HARV_landRGB.3 - b[1])/(b[2] - b[1])
tempR[tempR < 0] <- 0
tempG[tempG < 0] <- 0
tempB[tempB < 0] <- 0
tempR[tempR > 1] <- 1
tempG[tempG > 1] <- 1
tempB[tempB > 1] <- 1
RGB_133_df$rgb <- rgb(tempR,tempG,tempB)
ggplot() +
geom_raster(data = RGB_133_df, aes(x, y), fill = RGB_133_df$rgb) +
ggtitle("Julian day 133")
# Plot RGB data for Julian day 197
RGB_197 <- stack("data/NEON-DS-Landsat-NDVI/HARV/2011/RGB/197_HARV_landRGB.tif")
RGB_197 <- RGB_197/255
RGB_197_df <- raster::as.data.frame(RGB_197, xy = TRUE)
r <- quantile(RGB_197_df$X197_HARV_landRGB.1, quantiles, na.rm = TRUE)
g <- quantile(RGB_197_df$X197_HARV_landRGB.2, quantiles, na.rm = TRUE)
b <- quantile(RGB_197_df$X197_HARV_landRGB.3, quantiles, na.rm = TRUE)
tempR <- (RGB_197_df$X197_HARV_landRGB.1 - r[1])/(r[2] - r[1])
tempG <- (RGB_197_df$X197_HARV_landRGB.2 - g[1])/(g[2] - g[1])
tempB <- (RGB_197_df$X197_HARV_landRGB.3 - b[1])/(b[2] - b[1])
tempR[tempR < 0] <- 0
tempG[tempG < 0] <- 0
tempB[tempB < 0] <- 0
tempR[tempR > 1] <- 1
tempG[tempG > 1] <- 1
tempB[tempB > 1] <- 1
RGB_197_df$rgb <- rgb(tempR,tempG,tempB)
ggplot() +
geom_raster(data = RGB_197_df, aes(x, y), fill = RGB_197_
df$rgb) +
ggtitle("Julian day 197")
Refs to a comment in the closing of #224
@fmichonneau - since this code is only in the demonstration (and the code isn't shown to learners), this issue does not need to be resolved before lesson release.