为图制作2D图例-双变量Choropleth贴图 [英] Make a 2D legend for a plot - bi-variate choropleth maps
本文介绍了为图制作2D图例-双变量Choropleth贴图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
I have been playing with bi-variate choropleth maps and have gotten stuck on how to create a 2d legend similar to the one by Joshua Stevens shown here:
为说明这一挑战,我们不需要使用地图.下面的代码就足够了:
To illustrate the challenge we don't need to use a map. The code below will suffice:
#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
它将创建一个类似于我要使用的图例的图,但是图例当然是所有级别的垂直条.我希望图例看起来像情节本身.有没有办法做到这一点?谢谢!
It creates a plot that looks like the legend I am going for but of course the legend is a vertical bar of all the levels. I want the legend to look like the plot itself. Is there a way to do that? Thanks!
推荐答案
Not a complete answer, but I think you need to play with guides.
ggplot(legendGoal,
aes(Var1,Var2,
col=as.factor(value),
fill=as.factor(value))) +
geom_tile() +
guides(col = guide_legend(nrow = 3))
这篇关于为图制作2D图例-双变量Choropleth贴图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文