我想修改我的
读取它并快速查看它似乎所有信息都在那里。
.tif
文件中的颜色。我已经将文件上传在这里。读取它并快速查看它似乎所有信息都在那里。
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)
到目前为止一切都很好。现在我想手动更改颜色,因此我将对象转换为data.frame
以便使用ggplot2
。但是,在某个地方,我正在丢失信息。有人知道如何解决这个问题吗?
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()
这按预期工作。但当指定 scale_fill_manual
时,我得到了一个奇怪的图形,表明在提取颜色时出现了错误: 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()
我该如何正确访问似乎存在的颜色(plotRGB
输出的结果)?谢谢!