# read shapefile of boston precincts
boston <- fortify(
readOGR(dsn='WDPC_2015', layer='WDPC_2015'), region='WARD_PRECI')
## OGR data source with driver: ESRI Shapefile
## Source: "WDPC_2015", layer: "WDPC_2015"
## with 255 features
## It has 7 fields
# read csv of boston precinct election results,
# rename precinct to id,
# create a new column: no percent - yes percent,
# and only look at precincts with more than one vote
results <- read.csv('./boston-question-2.csv') %>%
rename(id = precinct) %>%
filter(total.votes > 1) %>%
mutate(pct = ifelse(yes.pct > 50, yes.pct, -no.pct))
# create intervals
results.intervals <- classIntervals(results$pct, style = 'fixed',
fixedBreaks = c(-80, -75, -70, -65, -60, -55, -50, 50, 55, 60, 65, 70))$brks
# bin data into intervals
results$breaks <- cut(results$pct, breaks=results.intervals, include.lowest = TRUE)
# join shapefile with results
data <- left_join(boston, results)
# create color ramp
colors <- c(
'#393d41',
'#733236',
'#aa2b31',
'#db6950',
'#f79659',
'#ffce7a',
'#d9e2cc',
'#b7c28c',
'#9dae77',
'#66724d'
)
# make plot
ggplot() +
geom_polygon(data=data, aes(x=long, y=lat, group=group, fill=breaks)) +
ggtitle('Margin of victory for ballot question 2') +
coord_map() +
scale_fill_manual(values=colors) +
theme_void()
