Tuesday, November 5, 2013

R - GGPLOT2 - State Map with Custom Legend

I spent some time making a few Indiana county maps in R. I used ggplot2 to create the map and Colorbrewer2.0 for the colors. As the title to this post indicates, I had to create a custom legend for one of the maps. The two related maps are counts of insurance companies who are offering plan under the ACA healthcare exchange. The color of the county will vary depending on the number of insurers offering plans. The first of the two is for the individual insurance market the second is for the SHOP market. (Small Groups).  The purpose for a custom legend is to should levels of colors on both maps even though the second map has a maximum lower than the first map.

Code for these maps can be found below with some limited commenting. I have not found away to create a legend on its own without tying it back to some geom_xxx layer. So to produce a legend that illustrated the full range of data across multiple maps, I created two layers. The lower layer is fake data which creates the full legend and then the top layer plots the correct data on to the map.


#Packages
require(ggplot2);require(mapproj);require(RColorBrewer)
 
#Create original map, place it on the Mercator projection, clean up the grid
IN.build <- ggplot(indiana.data, aes(long, lat)) +
                    coord_map() +
                    theme_bw() +
                    theme(#legend.position = "none"
                      axis.title=element_blank()
                      ,axis.ticks=element_blank()
                      ,axis.text=element_blank()
                      ,panel.grid=element_blank()
                      ,panel.border=element_blank())
 
#Add bottom layer to map to create the legend I want
#I want a legend for values of 0 to 4. 1 through 4 will be colored from colorbrewer 
##and 0 will be a grey.
#Notice the fill on the polygon is just repeating 0:4.
IN.build <- IN.build +
                    geom_polygon(aes(group=group, fill=factor(rep(0:4
                                           ,length.out=nrow(indiana.data)))),color=NA) +
                    scale_fill_manual(name='# of Insurers\nAvailable'
                       ,values=c("grey40",brewer.pal(tot.poss.Insurers,'BuGn'))
                       ,na.value="grey40"
                       ,breaks=rev(levels(indiana.data$count.ind))
                       ,labels=rev(levels(indiana.data$count.ind)))
 
#Now it is time to create the top layer with the data we are interested in
#NOTICE: color=NA and geom_path(...), this combo removes the slashes 
##through the squares on the legend.
IN.build <- IN.build +
                    geom_polygon(aes(group=group, fill=count.ind),color=NA) +
                    geom_path(aes(group=group))

No comments:

Post a Comment