Last active
December 15, 2016 09:55
-
-
Save n8thangreen/8802472 to your computer and use it in GitHub Desktop.
Plot UK choropleth maps developed using LA and ETS data but can be applied more generally
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| map.tb <- function(map.data, g=NA, brks=NA, title="", value="tb", file=FALSE){ | |
| ## plot UK choropleth maps | |
| ## developed using LA and ETS data but | |
| ## can be applied more generally | |
| ## | |
| ## map.data: map object loaded by readOGR() | |
| ## g: number of groups for values; max(g)=9 | |
| ## brks: given break points to group values | |
| ## title: plot title | |
| ## value: variable name of colouring value (string) | |
| ## file: output to file (logical) | |
| if(file){png(file=paste("figures/", title, ".png", sep=""), width = 600, height= 550)} | |
| par(mfrow=c(1,1)) | |
| par(mar=c(0.1, 0.1, 1.5, 1.5)) | |
| if(!is.na(brks)){ | |
| ## defined breakpoints | |
| cols <- brewer.pal(length(brks), "Greens") | |
| frac <- cut(map.data@data[,value], brks) | |
| gs <- cols[findInterval(map.data@data[,value], vec = brks)] | |
| plot(map.data, col = gs) | |
| legend("topright", legend = levels(frac), fill = cols, title=title) | |
| } | |
| else if(!is.na(g)){ | |
| ## defined number of groups | |
| cols <- brewer.pal(g, "Greens") | |
| frac <- cut2(map.data@data[,value], g=g) | |
| gs <- cols[as.integer(factor(frac))] | |
| plot(map.data, col = gs) | |
| legend("topright", legend = levels(frac), fill = cols, title=title) | |
| } | |
| if(file){dev.off()} | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment