Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#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
You can’t perform that action at this time.