使用“蒙版”功能根据多边形切割光栅后:
ras <- mask(ras0, polygon)
我想用ggplot呈现栅格(下面的链接)。但是,我的切割光栅外部的“NA”值存在问题。
https://depots.univ-perp.fr/get?k=9sh9zKXDpRTkVQslvJk
我在“scale_fill_manual”中添加选项na.value =“transparent”,将NA值透明地放在我的地图上,但NA的图例始终保留!
如何在图例中删除文字“NA”和灰色对应的方框?
当使用“掩码”功能或使用“Writeraster”注册时,是否有解决方法可以去除NA值,以避免在使用ggplot显示时出现此问题?
以下是用于显示地图的程序:
library(raster)
ras<-raster("ras.tif")
# map
gplot(ras)+
geom_tile(aes(fill=factor(value, labels=c("A", "B", "C", "D", "E", "F","G"))))+
scale_fill_manual(values = c("red", "#22751a", "#48c665", "#d3d532", "#d78d0d", "#f6e600","#65d6ef"),
name= "Legend", na.value="transparent")+
coord_cartesian(xlim = c(-7, 12),ylim = c(32, 38)) +
#bg
theme(panel.background = element_rect(colour = "black", fill="lightblue"))
提前谢谢
答案 0 :(得分:0)
似乎scale_fill_manual
在使用因素时显示NA值。定义您真正想要展示的因素的方法是使用其他参数breaks
和labels
以及对应的向量。如果我使用你的代码,那就是:
# Vector of correspondence
cols <- c("A" = "red", "B" = "#22751a", "C" = "#48c665",
"D" = "#d3d532", "E" = "#d78d0d", "F" = "#f6e600","G" = "#65d6ef")
# plot
gplot(ras) +
geom_tile(aes(fill=factor(value, labels=c("A", "B", "C", "D", "E", "F","G"))))+
scale_fill_manual(values = cols,
breaks = c("A", "B", "C", "D", "E", "F","G"),
labels = c("A", "B", "C", "D", "E", "F","G"),
name= "Legend") +
coord_cartesian(xlim = c(-7, 12),ylim = c(32, 38)) +
theme(panel.background = element_rect(colour = "black", fill="lightblue"))
但是,您可以以另一种方式使用它,同时将光栅的值(作为字符)直接归因于特定颜色然后命名(我必须添加新颜色,因为您的栅格中有8个级别):
# Vector of correspondence
cols.nb <- c("0" = "blue", "1" = "red", "2" = "#22751a", "3" = "#48c665",
"4" = "#d3d532", "5" = "#d78d0d", "6" = "#f6e600",
"7" = "#65d6ef")
# Plot
gplot(ras) +
geom_tile(aes(fill = as.character(value))) +
scale_fill_manual(values = cols.nb,
breaks = 0:7,
labels = c("A", "B", "C", "D", "E", "F","G","H"),
name = "Legend") +
coord_cartesian(xlim = c(-7, 12),ylim = c(32, 38)) +
theme(panel.background = element_rect(colour = "black", fill="lightblue"))