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