为绘图制作 2D 图例 - 双变量等值线图

Art*_*Art 5 gis plot r ggplot2

我一直在玩双变量 choropleth 地图,并且一直在研究如何创建类似于Joshua Stevens在这里显示的那个的 2d 图例:

在此处输入图片说明

为了说明这一挑战,我们不需要使用地图。下面的代码就足够了:

#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
Run Code Online (Sandbox Code Playgroud)

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

zx8*_*754 2

不是一个完整的答案,但我认为你需要使用指南

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

在此输入图像描述

  • 指导!这就是门票和 ggplot2 中我没有探索过的区域。你的解决方案已经差不多了。“col”与“fill”相对,带有参考线,排除在示例中使用手动颜色。在错误地进行了一些排列之后,我想出了下面的解决方案。谢谢! (2认同)