I'm trying to modify the colors within my .tif. I've uploaded a file here.
Reading it in and quickly looking at it seems like all the information is there.
library(dplyr);library(ggplot2)
library(raster)
pic <- raster::brick(x="SUB_IMG_8020 (1)_A_36x36.tif")
pic
class : RasterBrick
dimensions : 619, 1060, 656140, 3 (nrow, ncol, ncell, nlayers)
resolution : 1, 1 (x, y)
extent : 0, 1060, 0, 619 (xmin, xmax, ymin, ymax)
crs : NA
source : SUB_IMG_8020 (1)_A_36x36.tif
names : SUB_IMG_8020_.1._A_36x36.1, SUB_IMG_8020_.1._A_36x36.2, SUB_IMG_8020_.1._A_36x36.3
min values : 0, 0, 0
max values : 255, 255, 255
plotRGB(pic)
So far so good. Now I want to manually change colors and hence, I transform the object to a data.framein order to use ggplot2. However, somewhere along the way I'm loosing information. Does anyone have an idea how to solve this?
test_spdf <- as(pic, "SpatialPixelsDataFrame")
#extract colors
test_df <- as.data.frame(test_spdf) %>%
mutate(cols = rgb(SUB_IMG_8020_.1._A_36x36.1,
SUB_IMG_8020_.1._A_36x36.2,
SUB_IMG_8020_.1._A_36x36.3,
maxColorValue = 255)) %>%
dplyr::select(x, y, cols) %>% arrange(x) %>%
tibble::rowid_to_column("nr")
ggplot(test_df, aes(x=x, y=y)) +
geom_raster(aes(fill=cols))+
theme_void()+
theme(legend.position="none")+
coord_fixed()
This works out as expected. But when specifying scale_fill_manual I'm getting a weird looking plot which suggests that something went wrong when extracting the colors:
ggplot(test_df, aes(x=x, y=y)) +
geom_raster(aes(fill=cols))+
scale_fill_manual(values=c(test_df$cols))+
theme_void()+
theme(legend.position="none")+
coord_fixed()
How can I correctly access the colors which somehow seem to be present (output of plotRGB). Thank you!


