打印缺少数据的地图时遇到了麻烦。
我能够制作一个空的" shape文件:
empty.shape.sf <-
ggplot(BiH.shape.sf)+
geom_sf(fill="grey",colour="black")+
theme(legend.position="none",
axis.title=element_blank(),
axis.text=element_blank(),
axis.ticks = element_blank(),
strip.text.y = element_text(size = 10),
panel.background=element_rect(fill="white"))
print(empty.shape.sf)
df.shape <- dplyr::left_join(BiH.shape.sf, data, by="ID_3")
并制作新地图。
data.map <- df.shape%>%
filter(year==2000|year==2004)%>%
ggplot()+
geom_sf(aes(fill=res), colour="black")+
theme(legend.position="none",
axis.title=element_blank(),
axis.text=element_blank(),
axis.ticks = element_blank(),
strip.text.y = element_text(size = 10),
panel.background=element_rect(fill="white"))+
scale_fill_gradient(low="blue", high="red", limits=c(0,100))+
facet_wrap(~year)
print(data.map)
为什么没有边框/丢弃投影数据的区域?我原以为使用left_join保留所有边框/区域。我该如何保留这些边界/区域?没有其他方法可以创造一个完整的&#39;数据集,其中包含每个缺失区域具有NA的行?
答案 0 :(得分:3)
我认为您可以简单地将参数na.value
添加到您对scale_fill_gradient的调用中。以下是使用sf
包中包含的North Carolina shapefile的可重现示例:
library(ggplot2)
library(sf)
nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
nc$BIR74[1] <- NA
nc %>% ggplot()+
geom_sf(aes(fill=BIR74), colour="white")+
theme(legend.position="none",
axis.title=element_blank(),
axis.text=element_blank(),
axis.ticks = element_blank(),
strip.text.y = element_text(size = 10),
panel.background=element_rect(fill="white"))+
scale_fill_gradient(low="blue", high="red",na.value = "black")
由于我无法重现您的特定示例,我猜您想要使用不同类型的join
,否则shapefile将保留那些缺失的行(和缺少的形状),如上所述。