Skip to content

Instantly share code, notes, and snippets.

@earino
Created August 3, 2014 20:32
Show Gist options
  • Save earino/ab49e04282f047eedfde to your computer and use it in GitHub Desktop.
Save earino/ab49e04282f047eedfde to your computer and use it in GitHub Desktop.
joke_map.R
joke_map <- function(states_to_highlight, title="", highlight_colors=c("#aaaaaa", "#ee0000")) {
library(dplyr)
library(ggplot2)
library(RColorBrewer)
library(maps)
highlighting = data.frame(region=tolower(state.name[match(states_to_highlight, state.abb)]),
highlight=TRUE)
d = map_data("state") %>% left_join(highlighting, by="region")
d[is.na(d$highlight),]$highlight <- FALSE
p <- ggplot(d) + geom_polygon(aes(x=long, y=lat,
group = group, fill=highlight),
colour="black") +
scale_fill_manual(values=highlight_colors) +
ggtitle(title) +
xlab("") +
ylab("") +
theme(axis.ticks = element_blank(), axis.text.x = element_blank(), axis.text.y = element_blank()) +
guides(fill=FALSE)
p
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment