绘制一个二维图例来表示双变量区域填充地图中的数据。

5

我一直在尝试制作双变量等值线地图,并卡在如何创建类似Joshua Stevens所示的二维图例上:

enter image description here

为了说明问题,我们不需要使用地图。以下代码足以说明:

#test desired legend appearance
library(ggplot2)
library(reshape2)
#use color scheme shown here http://www.joshuastevens.net/cartography/make-a-bivariate-choropleth-map/
bvColors=c("#be64ac","#8c62aa","#3b4994","#dfb0d6","#a5add3","#5698b9","#e8e8e8","#ace4e4","#5ac8c8")
legendGoal=melt(matrix(1:9,nrow=3))
test<-ggplot(legendGoal, aes(Var1,Var2))+ geom_tile(aes(fill = as.factor(value)))
test<- test + scale_fill_manual(name="Var1 vs Var2",values=bvColors,drop=FALSE)
test

它创建了一个看起来像我想要的图例的图,但是当然图例是所有级别的垂直条形。我希望图例看起来像图本身。有没有办法做到这一点?谢谢!

它创建的绘图看起来像我想要的图例,但是图例实际上是一个包含所有级别的垂直条形。我希望图例看起来与图本身类似。是否有方法可以实现这一点?谢谢!
3个回答

4
#test desired legend appearance
library(ggplot2)
library(reshape2)
#use color scheme shown here http://www.joshuastevens.net/cartography/make-a-bivariate-choropleth-map/
bvColors=c("#be64ac","#8c62aa","#3b4994","#dfb0d6","#a5add3","#5698b9","#e8e8e8","#ace4e4","#5ac8c8")
melt(matrix(1:9,nrow=3))
legendGoal=melt(matrix(1:9,nrow=3))
test<-ggplot(legendGoal, aes(Var2,Var1,fill = as.factor(value)))+ geom_tile()
test<- test + scale_fill_manual(name="More Var2  -->",values=bvColors,drop=FALSE)
test<-test+guides(fill = guide_legend(nrow = 3))
test<-test + theme(legend.text=element_blank())

test

进入图像描述

唯一的问题是如何在图例侧面添加垂直文本 "More Var1 -->。" 这里有一个丑陋的方法:

test<-ggplot(legendGoal, aes(Var2,Var1,fill = as.factor(value)))+ geom_tile()
test<- test + scale_fill_manual(name="More Var2  -->",values=bvColors,labels=c("","","","","","","More","Var 1"," v "))
test<-test+guides(fill = guide_legend(nrow = 3))
#test<-test + theme(legend.text=element_blank())

test

然而,正如zx所展示的那样,使用cowplot包扩展ggplot2是完整的解决方案:

#test desired legend appearance
library(ggplot2)
library(cowplot)
library(reshape2)
#use color scheme shown here http://www.joshuastevens.net/cartography/make-a-bivariate-choropleth-map/
bvColors=c("#be64ac","#8c62aa","#3b4994","#dfb0d6","#a5add3","#5698b9","#e8e8e8","#ace4e4","#5ac8c8")
melt(matrix(1:9,nrow=3))
legendGoal=melt(matrix(1:9,nrow=3))
test<-ggplot(legendGoal, aes(Var2,Var1,fill = as.factor(value)))+ geom_tile()
test<- test + scale_fill_manual(name="",values=bvColors)
test<-test+guides(fill = guide_legend(nrow = 3))
test<-test + theme(legend.text=element_blank())
test<-ggdraw(test) + draw_text(text = "More Var 2 -->",x=0.91,y=0.58)
test<-ggdraw(test) + draw_text(text = "More Var 1 -->",x=0.84,y=0.5,angle=270)
test

enter image description here

仅供娱乐,这是我用这种技术制作的地图: NYC Pre-K Seats


2

虽然不是完整的答案,但我认为你需要使用guides来进行一些调整。

ggplot(legendGoal, 
       aes(Var1,Var2,
           col=as.factor(value),
           fill=as.factor(value))) +
  geom_tile() +
  guides(col = guide_legend(nrow = 3))

enter image description here


2
指南!这是一个我还没有探索过的ggplot2领域。你的解决方案已经接近成功了。在你的示例中,使用“col”而不是“fill”来预先排除手动颜色。在一些尝试后,我想出了下面的解决方案。谢谢! - Art

2

好的,最后一次更新。列之间的空白让我感到困扰,使用cowplot的网格功能将允许我们将缩小的绘图用作双变量图例,如下所示:

library(ggplot2)
library(cowplot)
library(reshape2)
#use color scheme shown here http://www.joshuastevens.net/cartography/make-a-bivariate-choropleth-map/
bvColors=c("#be64ac","#8c62aa","#3b4994","#dfb0d6","#a5add3","#5698b9","#e8e8e8","#ace4e4","#5ac8c8")
melt(matrix(1:9,nrow=3))
legendGoal=melt(matrix(1:9,nrow=3))
test<-ggplot(legendGoal, aes(Var2,Var1,fill = as.factor(value)))+ geom_tile()
test<- test + scale_fill_manual(name="",values=bvColors)
test<-test + theme(legend.position="none")
#test<-ggdraw(test) + draw_text(text = "More Var 2 -->",x=0.91,y=0.58)
#test<-ggdraw(test) + draw_text(text = "More Var 1 -->",x=0.84,y=0.5,angle=270)

#create a plot that will be the legend itself
lg<- test #would not be true when making a map
lg<-lg + theme(axis.title.x=element_text(size=rel(1),color=bvColors[3])) + xlab("More Var2 -->")
lg<-lg + theme(axis.title.y=element_text(size=rel(1),color=bvColors[3])) + ylab("More Var1 -->")
lg<-lg+theme(axis.text=element_blank())
lg<-lg+theme(line=element_blank())
#put both plots on a grid
ggdraw()+ draw_plot(lg,0.1,0.7,width=0.2,height=0.2) +draw_plot(test,0.3,0,width=.7,height=.7)

这里输入图片描述 相当漂亮,是吧?


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接