Skip to content

Instantly share code, notes, and snippets.

@rudeboybert
Last active October 12, 2016 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rudeboybert/1837a4d79bfae938246b662fc15629cd to your computer and use it in GitHub Desktop.
Save rudeboybert/1837a4d79bfae938246b662fc15629cd to your computer and use it in GitHub Desktop.
Map of states that switched between Trump/Clinton if genders voted separately
# Which states changed colors here:
# http://fivethirtyeight.com/features/election-update-women-are-defeating-donald-trump/
library(ggplot2)
library(maps)
library(mapproj)
library(dplyr)
# Load map of us data
all_states <- map_data("state")
# Define data frame of states that switched color
states <- all_states$region %>%
unique() %>%
sort() %>%
setdiff(c("district of columbia","alaska", "hawaii"))
switch_states <- c(
"new hampshire", "pennsylvania", "ohio", "virginia", "indiana", "michigan", "arizona", "nevada",
"montana", "colorado", "texas", "south dakota", "kansas", "missouri", "iowa",
"wisconsin", "florida", "georgia", "south carolina", "north carolina", "minnesota"
)
switch <- data_frame(
state = states,
switch_state = is.element(states, switch_states)
)
# Create two separate maps
all_states <- all_states %>%
left_join(switch, by=c('region'='state'))
all_states_sub <- all_states %>%
filter(switch_state)
ggplot() +
geom_polygon(data=all_states_sub, aes(x=long, y=lat, group = group), fill="cyan" ) +
geom_path(data=all_states, aes(x=long, y=lat, group = group), colour="black" ) +
coord_map() +
labs(title="States that Switched Between Genders")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment