Luckily R includes a package that makes this quite simple. The scales package includes the very useful alpha() function which transforms a color value using some scalar modifier. To achieve this in the scripts I used for the last post I simply had to create some scalar and use that to modify the existing color scheme.
This only adds two additional lines to the earlier scripts:
#Calculate winning percentage to use for shading elect12$WinPct <- elect12$Win_Votes/elect12$TOTAL.VOTES.CAST #Create transparent colors using scales package elect12$alphaCol <- alpha(elect12$col,elect12$WinPct) #Match colors to county.FIPS positions
I really think this helps add another dimension to the chart, answering an inevitable question that the reader might have.
There are a couple of issues with this methodology however, since the observed winning percentage values are so centered around certain values. In mid fifties for many counties, 2012 range was 46.2% (Eastford county Connecticut) to 95.9% (King county Texas). This causes a washout effect on the colors in the chart. A non-linear scaling using a log scale or binned color mapping could help with this.
Additionally, some measure of population size could be added to improve the readability of the chart. Election maps (as with most US level value maps) suffer from the cognitive dissonance of a seemingly uniform land distribution with a disparate population distribution. I was really influenced by Mark Newmans' fun take on election mapping. The cartographs he posted are especially interesting, I hope to create those in R sometime. I love the way that the population weighted cartograph allows the reader to intuit the average value from an otherwise misleading two color heatmap.
No comments:
Post a Comment