# Show Virginia registered voters with census population. # Franklin Center for Government & Public Integrity # Earl F Glynn # 28 July 2012. Update 20 Aug 2012. ################################################################################ ### Setup # Common functions to read/plot voting age census data by county source("F:/US-Census/Voting-Age-Population/Census-Voting-Age-Population.R") setwd("F:/Voter-Registration/Virginia/census-registration-comparison/") # CapLeading attributed to Christian Hoffmann in R mailing list CapLeading <- function(string) { fn <- function(x) { v <- unlist(strsplit(x, split = " ")) u <- sapply(v, function(x){ x <- tolower(x) substring(x, 1, 1) <- toupper(substring(x, 1, 1)) x}) paste(u, collapse = " ") } sapply(string, fn) } ################################################################################ ### Virginia county plotting callback function to plot voter registration # data known globally plot.virginia.registration <- function(county) { county.name <- CapLeading(county) if (county.name == "Total") county.name <- "TOTAL" # CapLeading messed this one up if (county.name == "Isle Of Wight County") county.name <- "Isle of Wight County" if (county.name == "King And Queen County") county.name <- "King and Queen County" county.data <- subset(registered, registered$Locality == county.name) points(county.data$Date, county.data$Total, pch=3, cex=1.25, col="red") points(county.data$Date, county.data$Active, pch=4, cex=1.25, col="green") points(voter.file.date, voter.file[county.name, "Total"], pch=16, col="black") points(voter.file.date, voter.file[county.name, 1], pch=16, col="black") text(voter.file.date, voter.file[county.name, "Total"] - strheight("X"), "voter file", adj=0.5, col="black") text(voter.file.date, voter.file[county.name, "Active"] - strheight("X"), "voter file", adj=0.5, col="black") mtext(" Sources: U.S. Census; Virginia Board of Elections", BOTTOM<-1, adj=0, line=-1, cex=0.75, col="blue", outer=TRUE) mtext("2012-08-23", BOTTOM<-1, adj=0.98, line=-2, cex=0.5, col="grey", outer=TRUE) legend("bottom", c("Total Registration", "'Active' Voters"), title="Registered Voters", pch=3:4, cex=1.25, text.col=c("red", "green"), col=c("red","green"), box.lty="blank") } ################################################################################ ### Read Virginia Registered Voter data ### Remember: Need to have rowname of total line "TOTAL" for use in callback # County voter counts from Virginia Secretary of State registered <- read.csv("Virginia-Voter-Registration-2004-2012.csv", as.is=TRUE) registered$Date <- as.Date(registered$Date) # Voter file voter.file <- read.csv("Virginia-Active-Inactive-2012-04-16.csv", as.is=TRUE, row.names=1) rownames(voter.file) <- CapLeading(rownames(voter.file)) # Kludge fixes: # Fix things CapLeading messed up rownames(voter.file)[ 64] <- "Isle of Wight County" rownames(voter.file)[135] <- "TOTAL" # Keep this upper case # Get rid of ampersand in name rownames(voter.file)[66] <- "King and Queen County" voter.file.date <- as.Date("2012-04-16") ################################################################################ ### Plot Virginia census and voter registration data by county. # Virginia FIPS code is "51" d <- get.voting.age.census.data("51") cat(d$state, "\n") # verify we have the correct state # Verify County names from census match county names of voter counts #stopifnot( all( gsub(" County","",d$counties) == rownames(registered) ) ) #### # Write census data file # Add actual dates instead of only year index before writing file d$census.data$Date <- d$timeline[d$census.data$Year] write.csv(d$census.data[,c("State", "County", "Year", "Date", "Population", "VotingAge")], paste(gsub(" ","-",d$state), "-Voting-Age-Population.csv", sep=""), row.names=FALSE) # Plot all counties in PDF file pdf(paste(gsub(" ","-",d$state), # Change blanks to dashes in state names "-Voting-Age-Population-and-Registered-Voters.pdf", sep=""), width=8, height=8) plot.county.population.charts(d$state, d$counties, d$census.data, d$timeline, plot.virginia.registration) dev.off()