12.6 Graph for a smaller set of results
This Massachusetts data had more than 350 results, meaning visualization choices such as bar graphs might not make sense. But when covering precinct results in one town, or results by town for just a handful of places, you might want a bar chart where the winner’s bars are one color and the loser’s are another.
This could also be useful for a local reporter covering a state-wide race who wants to examine one specific area’s results, such as Cape Cod. The next code block subsets the data for towns on Cape Cod, and creates a bar graph with different colors for Baker and Coakley towns.
The first line defines all the towns I want in this Cape Cod graph and stores them in a variable
cape_cod_towns. The second line keeps only those rows in the winner’s data frame where the Place column is one of the cape_cod_towns, and stores them in a new variable called cape. Finally, the third statement creates a bar chart of Baker’s winning percent margin in those towns, where the bar colors are based on the Winner column.
cape_cod_towns <- c("Falmouth", "Bourne", "Mashpee", "Sandwich", "Barnstable", "Hyannis", "Yarmouth", "Dennis", "Harwich", "Brewster", "Chatham", "Orleans", "Eastham", "Wellfleet", "Truro", "Provincetown") cape <- filter(winners, Place %in% cape_cod_towns) ggplot(cape, aes(x=Place, y=Baker.pct.margin, fill=Winner)) + geom_col() + labs(title = ma_gov_headline, caption = ma_gov_datasource) + theme(axis.text.x = element_text(angle = 45, vjust = 1.2, hjust = 1.1))
Figure 12.10: Bar chart with winners and losers by town.
This ggplot2 (Figure 12.10) happens to set the Republican candidate in red and Democrat in blue, but that won’t always be the case. You can set your own fill color scheme with scale_fill_manual() using syntax something like
There are other exploratory graphics you might want to generate from election results. If you’re covering a city election with 15 or 20 precincts, a bar chart of margins per precinct might be interesting. In a tight election, you might want to examine totals for third-party candidates and blanks versus the winning candidate’s margins. This is just a starting point for you to create your own election recipes, based on topics that might be interesting in the area that you cover.