Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a 2D legend for a plot - bi-variate choropleth maps

Tags:

plot

r

ggplot2

gis

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:

enter image description 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!

like image 529
Art Avatar asked Sep 23 '15 21:09

Art


1 Answers

#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

enter image description here

The only remaining trick is to find a way to add some vertical on the side of the legend saying "More Var1 -->." Here's a butt-ugly way to do it:

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

But, as zx shows, extending ggplot2 with the cowplot package is the complete solution:

#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

Just for fun this is the map that I made with this technique: NYC Pre-K Seats

like image 148
Art Avatar answered Nov 15 '22 05:11

Art