r-raster-vector-geospatial
r-raster-vector-geospatial copied to clipboard
Cleaning up some RGB value manipulation in Episode 12
I made this code look nicer using mutate(), case_when(), and select() from dplyr. This was marked as a "good first issue" in Issue #227 , so I thought I would tackle it.
Thanks for this! Have you run the code to make sure it works? It might just be me but I get an error in the first part of the mutate
chain:
RGB_133 <- stack("data/NEON-DS-Landsat-NDVI/HARV/2011/RGB/133_HARV_landRGB.tif")
RGB_133_df <- as.data.frame(RGB_133, xy = TRUE)
quantiles = c(0.02, 0.98)
RGB_133_df <- RGB_133_df %>%
mutate(r = quantile(X133_HARV_landRGB.1, quantiles, na.rm = TRUE))
Error in mutate_impl(.data, dots) : Column
r
must be length 453792 (the number of rows) or one, not 2
Reviewing open PR's in response to #369:
I haven't run this again with the 3 commits made after my initial response. Assuming the code now runs, it seems to me the key consideration in merging this is whether we want to introduce a new dependency (tidyr
).
@jsta I agree the main discussion point is whether or not to add library tidyr
to mix. This also removes the need for reshape
package in episode 12. Thoughts?
I was able to get the code to run but had to change the .
to underscore to address invalid layer names inside the quantile
function. Note: I had to do this in the code as it currently sits in our repo as well.
# code not shown, demonstration only
# Plot RGB data for Julian day 133
RGB_133 <- stack("data/NEON-DS-Landsat-NDVI/HARV/2011/RGB/133_HARV_landRGB.tif")
quantiles = c(0.02, 0.98)
r <- quantile(RGB_133$X133_HARV_landRGB_1, quantiles, na.rm = TRUE)
g <- quantile(RGB_133$X133_HARV_landRGB_2, quantiles, na.rm = TRUE)
b <- quantile(RGB_133$X133_HARV_landRGB_3, quantiles, na.rm = TRUE)
RGB_133_df <- as.data.frame(RGB_133, xy = TRUE) %>%
mutate(tempR = (X133_HARV_landRGB_1 - r[1])/(r[2] - r[1]),
tempG = (X133_HARV_landRGB_2 - g[1])/(g[2] - g[1]),
tempB = (X133_HARV_landRGB_3 - b[1])/(b[2] - b[1]),
tempR = case_when(
tempR < 0 ~ 0,
tempR > 1 ~ 1,
TRUE ~ tempR),
tempG = case_when(
tempG < 0 ~ 0,
tempG > 1 ~ 1,
TRUE ~ tempG),
tempB = case_when(
tempB < 0 ~ 0,
tempB > 1 ~ 1,
TRUE ~ tempB),
rgb = rgb(tempR,tempG,tempB)
) %>%
dplyr::select(-(tempR: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
r <- quantile(RGB_197$X197_HARV_landRGB_1, quantiles, na.rm = TRUE)
g <- quantile(RGB_197$X197_HARV_landRGB_2, quantiles, na.rm = TRUE)
b <- quantile(RGB_197$X197_HARV_landRGB_3, quantiles, na.rm = TRUE)
RGB_197_df <- as.data.frame(RGB_197, xy = TRUE) %>%
mutate(tempR = (X197_HARV_landRGB_1 - r[1])/(r[2] - r[1]),
tempR = case_when(
tempR < 0 ~ 0,
tempR > 1 ~ 1,
TRUE ~ tempR),
tempG = (X197_HARV_landRGB_2 - g[1])/(g[2] - g[1]),
tempG = case_when(
tempG < 0 ~ 0,
tempG > 1 ~ 1,
TRUE ~ tempG),
tempB = (X197_HARV_landRGB_3 - b[1])/(b[2] - b[1]),
tempB = case_when(
tempB < 0 ~ 0,
tempB > 1 ~ 1,
TRUE ~ tempB),
rgb = rgb(tempR,tempG,tempB)
) %>%
dplyr::select(-(tempR:tempB)) # get rid of the temporary variables we created.
ggplot() +
geom_raster(data = RGB_197_df, aes(x, y), fill = RGB_197_df$rgb) +
ggtitle("Julian day 197")
My personal preference is to use tidyr over reshape. So I say go for it.