Josh Stone, Blog

Josh’s projects and security nerdery

Experimenting With GNU R and Maps

I was playing around a little with some data from Gallup. In August, they did a poll to estimate Democrat / Republican bias on a per-state basis. I wanted to play with generating a colorful map with R, and here’s what I came up with:


Here’s approximately how I went about it:

bias  <- read.table("democrat-advantage-by-state.txt", header = TRUE, sep = ",")
dataByBias <- data[order(data$Bias),]
cutoffs <- quantile(dataByBias$Bias, seq(0,1,len=11))

hc <- colorRampPalette(c("#FF0000FF", "#FFFFFFFF", "#0000FFFF"))(7)[cut(dataByBias$Bias, cutoffs, ordered_result=T, include.lowest=T)]

map("state", interior=F, col="Black")
for(i in 1:length(dataByBias$Rate)) { map("state", regions=dataByBias$State[i], col=hc[i], add=T, fill=T) }

I exported the plot as an SVG and did some touchup in Inkscape and Gimp. I’m getting better at R!