基于两种不同条件的ggplot映射填充

ggplot mapping fill based on two different conditions

我想制作一张地图,其中各州根据一个标准着色,并根据不同的标准用交叉线(或以其他方式区分)。到目前为止我所拥有的是

library(ggmap)
library(mapdata)
library(ggplot2)
library(dplyr)
region<-c("california","nevada","oregon","washington")
var1<-c(0,1,0,1)
var2<-c(3,4,4,3)
my_data<-data.frame(region, var1, var2)
all_states_map <- map_data("state")
Total <- inner_join(all_states_map,my_data, by = "region")
Total$var1<-as.factor(Total$var1)
my_map <- ggplot() + geom_polygon(data=Total, aes(x=long, y=lat, group = Total$group, fill=Total$var1),colour="white",
                         show.legend=TRUE) + scale_fill_manual(values=c("blue","gray"))

所以现在我的状态根据 var1 是 0 还是 1 进行颜色编码。有没有办法在其上叠加一些东西以交叉影线 var2 为 3 的状态,而不管 var2 为 4 的状态?

编辑:我想要的是这样的:

这是否接近您希望看到的内容?

ggplot() + geom_polygon(data=Total, aes(x=long, y=lat, group = group, 
                        fill = paste(var1, var2, sep = ", ")), colour="white") + 
           scale_fill_manual(values=c("blue","gray", "green", "red")) + 
           labs(fill = "(var1, var2)")

另一种选择是将透明度设置为绘图的另一个维度,这样可能更接近您想要的?

ggplot() + geom_polygon(data=Total, aes(x=long, y=lat, group = group, 
                        fill = var1, alpha = as.factor(var2)), colour="white") + 
           scale_fill_manual(values=c("blue","red")) +
           scale_alpha_manual(values = c(0.5, 1)) + 
           labs(alpha = "var2", fill = "var1") 

在 ggplot 中似乎没有简单的解决方案。一种区分方法是在多段线周围使用不同的轮廓,如本例所示:

ggplot() + 
  geom_polygon(data=Total, aes(x=long, y=lat, group = Total$group, 
                              fill=Total$var1,colour=as.factor(var2)),
                              ,size=3,show.legend=TRUE) + 
  scale_fill_manual(values=c("blue","gray"))+
  scale_color_manual(values=c("black",NA))

但如您所见,由于多边形绘制顺序,这会导致加利福尼亚州和其他州之间的线宽不一致。我们可以通过增加 var2 等于 3:

Total2<-Total%>%
  mutate(group=ifelse(var2==3,group+100,group))

ggplot() + 
  geom_polygon(data=Total2, aes(x=long, y=lat, group = Total2$group, 
                               fill=Total2$var1,colour=as.factor(Total2$var2)),
               ,size=3,show.legend=TRUE) + 
  scale_fill_manual(values=c("blue","gray"))+
  scale_color_manual(values=c("black",NA))

如果你真的想要交叉影线,你可能不得不放弃这个 ggplot:

Fill Geospatial polygons with pattern - R

这可能不是很理想,但可能在某些方面有所帮助?

library(ggmap)
library(mapdata)
library(ggplot2)
library(dplyr)
library(sp)
region<-c("california","nevada","oregon","washington")
var1<-c(0,1,0,1)
var2<-c(3,4,4,3)
my_data<-data.frame(region, var1, var2)
all_states_map <- map_data("state")
Total <- inner_join(all_states_map,my_data, by = "region")
Total$var1<-as.factor(Total$var1)

randomPoints <- NULL

# For each region find a set of points inside each state
for(i in region){
  sub <- subset(Total, region == i)
  sub.sr = SpatialPolygons(list(Polygons(list(Polygon(sub[,1:2])), "a")))

  smple <- as.data.frame(spsample(sub.sr, n = 100, "regular"))

  temp <- data.frame(x = smple[,1], y = smple[,2], region = i, var1 = unique(sub$var1), var2 = unique(sub$var2))
  randomPoints <- rbind(randomPoints, temp)
}

ggplot() + 
  geom_polygon(data=Total, aes(x=long, y=lat, group = group, fill=var1), color = "white") + 
  geom_point(data = randomPoints, aes(x = x, y = y, shape = as.factor(var2)), size = 1) + 
  scale_shape_manual(values = c(1, 2))