Created
September 19, 2016 13:59
-
-
Save Yankim/ae93f25a1281c96582edb2b926188e3d to your computer and use it in GitHub Desktop.
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
#Generate County map with a few var of interest | |
percent_map <- function(var, color, legend.title, min = 0, max = 100, name = "") { | |
# generate vector of fill colors for map | |
shades <- colorRampPalette(c("white", color))(100) | |
# constrain gradient to percents that occur between min and max | |
var <- pmax(var, min) | |
var <- pmin(var, max) | |
percents <- as.integer(cut(var, 100, | |
include.lowest = TRUE, ordered = TRUE)) | |
fills <- shades[percents] | |
# plot choropleth map | |
map("county", fill = TRUE, col = fills, resolution = 0, | |
lty = 0, projection = "polyconic", | |
myborder = 0, mar = c(0,0,0,0), width = 5, height =4) | |
# overlay state borders | |
map("state", col = "black", fill = FALSE, add = TRUE, | |
lty = 1, lwd = 1, projection = "polyconic", | |
myborder = 0, mar = c(0,0,0,0), width = 5, height =4) | |
title(name) | |
# add a legend | |
inc <- (max - min) / 4 | |
legend.text <- c(paste0(min, " % or less"), | |
paste0(min + inc, " %"), | |
paste0(min + 2 * inc, " %"), | |
paste0(min + 3 * inc, " %"), | |
paste0(max, " % or more")) | |
legend("right", | |
legend = legend.text, | |
text.font = 12, | |
fill = shades[c(1, 25, 50, 75, 100)], | |
title = legend.title, | |
cex = 1.1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment