Monday, January 26, 2015

Some GIS electoral code examples: Part I


# Political piece is here.
# Code adapted from http://geog.uoregon.edu/GeogR/ GEOG 4/595 Geographic Data Analysis -- Winter 2015
# Dept. Geography, Univ. Oregon; last updated: 01/05/2015 11:58:39 AM; bartlein@uoregon.edu

library(plyr)
library(sp) 
library(rgdal)
library(maptools)  
library(RColorBrewer) # creates nice color schemes
library(classInt)     # finds class intervals for continuous variables
options(digits=3)

# returning data.frame from shape files
setwd("C:/Politics/WhatcomPrecincts")
WCshp <- readOGR("C:/Politics/WhatcomPrecincts","NewPrecincts2014")
plot(WCshp,col="red")

setwd("C:/Politics/BellinghamPrecincts")
COBshp <- readOGR("C:/Politics/BellinghamPrecincts","CityOfBellinghamVotingPrecincts2014")
plot(COBshp,col="blue")

# Matchbacks from 2014 Consolidated
setwd("C:/Politics")
MB_2014_Consolidated <- read.delim("MB_2014_Consolidated.txt", header = TRUE, strip.white = TRUE, sep = "\t", quote = "", stringsAsFactors = FALSE)
MB_2014_Consolidated_status <- subset(MB_2014_Consolidated, select = c(RegistrationNumber,PrecinctID,AVReturnStatus,AVReturnChallenge))
MB_2014_Consolidated_Good <- subset(MB_2014_Consolidated_status, AVReturnStatus == "Good")
MB2014TO <- arrange(data.frame(with(MB_2014_Consolidated_Good,(table(PrecinctID)))),(PrecinctID))

# Matchbacks from 2012 Consolidated
MB_2012_Consolidated <- read.delim("MBGE2012.txt", header = TRUE, strip.white = TRUE, sep = "\t", quote = "", stringsAsFactors = FALSE)
MB_Consolidated_status <- subset(MB_2012_Consolidated, select = c(RegistrationNumber,PrecinctID,AVReturnStatus,AVReturnChallenge))
MB_2012_Consolidated_Good <- subset(MB_Consolidated_status, AVReturnStatus == "Good")
MB2012TO <- arrange(data.frame(with(MB_2012_Consolidated_Good,(table(PrecinctID)))),(PrecinctID))

### WC Data
colnames(MB2012TO) <- c("NAME", "VotesGood")
MB2012TO$NAME <- as.numeric(as.character(MB2012TO$NAME))
WCshp_merge_2012 <- sp::merge(WCshp,MB2012TO,by="NAME")

colnames(MB2014TO) <- c("NAME", "VotesGood")
MB2014TO$NAME <- as.numeric(as.character(MB2014TO$NAME))
WCshp_merge_2014 <- sp::merge(WCshp,MB2014TO,by="NAME")

dataProjected_2014 <- WCshp_merge_2014
dataProjected_2012 <- WCshp_merge_2012

## Check 2014
dataProjected <- dataProjected_2014
# merge(subset(dataProjected@data,select=c(NAME,VotesGood)),MB2014TO,by="NAME")

# equal-frequency class intervals -- block 1
plotvar <- dataProjected@data$VotesGood
nclr <- 9
plotclr <- brewer.pal(nclr,"BuPu")
class <- classIntervals(plotvar, nclr, style="quantile")
colcode <- findColours(class, plotclr)
#This first block of code (above) just does some setting up, while this next block actually produces the map:

# block 2
plot(dataProjected) 
# xlim=c(-124.5, -115), ylim=c(42,47))
plot(dataProjected, col=colcode, add=T)
 title(main="Votes Good per Precinct in 2014 Whatcom County General Election",
   sub="Quantile (Equal-Frequency) Class Intervals")
 legend(locator(), legend=names(attr(colcode, "table")),
    fill=attr(colcode, "palette"), cex=0.6, bty="n")

## Check 2012
# merge(subset(dataProjected@data,select=c(NAME,VotesGood)),MB2012TO,by="NAME")
dataProjected <- dataProjected_2012
# equal-frequency class intervals -- block 1
plotvar <- dataProjected@data$VotesGood
nclr <- 9
plotclr <- brewer.pal(nclr,"BuPu")
class <- classIntervals(plotvar, nclr, style="quantile")
colcode <- findColours(class, plotclr)
#This first block of code (above) just does some setting up, while this next block actually produces the map:

# block 2
plot(dataProjected) 
# xlim=c(-124.5, -115), ylim=c(42,47))
plot(dataProjected, col=colcode, add=T)
title(main="Votes Good per Precinct in 2012 Whatcom County General Election",
    sub="Quantile (Equal-Frequency) Class Intervals")
legend(locator(), legend=names(attr(colcode, "table")),
    fill=attr(colcode, "palette"), cex=0.6, bty="n")
###COB data
colnames(MB2014TO) <- c("PRECINCT_N", "VotesGood")
MB2014TO$PRECINCT_N <- as.numeric(as.character(MB2014TO$PRECINCT_N))
COBshp_merge_2014 <- sp::merge(COBshp,MB2014TO,by="PRECINCT_N")

colnames(MB2012TO) <- c("PRECINCT_N", "VotesGood")
MB2012TO$PRECINCT_N <- as.numeric(as.character(MB2012TO$PRECINCT_N))
COBshp_merge_2012 <- sp::merge(COBshp,MB2012TO,by="PRECINCT_N")

dataProjected_2014 <- COBshp_merge_2014
dataProjected_2012 <- COBshp_merge_2012

## Check 2014
dataProjected <- dataProjected_2014
merge(subset(dataProjected@data,select=c(PRECINCT_N,VotesGood)),MB2014TO,by="PRECINCT_N")

# equal-frequency class intervals -- block 1
plotvar <- dataProjected@data$VotesGood
nclr <- 9
plotclr <- brewer.pal(nclr,"BuPu")
class <- classIntervals(plotvar, nclr, style="quantile")
colcode <- findColours(class, plotclr)
#This first block of code (above) just does some setting up, while this next block actually produces the map:

# block 2
plot(dataProjected) 
# xlim=c(-124.5, -115), ylim=c(42,47))
plot(dataProjected, col=colcode, add=T)
title(main="Votes Good per Bellingham Precincts in 2014 General Election",
    sub="Quantile (Equal-Frequency) Class Intervals")
legend(locator(), legend=names(attr(colcode, "table")),
    fill=attr(colcode, "palette"), cex=0.6, bty="n")
## Check 2012
dataProjected <- dataProjected_2012
merge(subset(dataProjected@data,select=c(PRECINCT_N,VotesGood)),MB2012TO,by="PRECINCT_N")

# equal-frequency class intervals -- block 1
plotvar <- dataProjected@data$VotesGood
nclr <- 9
plotclr <- brewer.pal(nclr,"BuPu")
class <- classIntervals(plotvar, nclr, style="quantile")
colcode <- findColours(class, plotclr)
#This first block of code (above) just does some setting up, while this next block actually produces the map:

# block 2
plot(dataProjected) 
# xlim=c(-124.5, -115), ylim=c(42,47))
plot(dataProjected, col=colcode, add=T)
title(main="Votes Good per Bellingham Precincts in 2012 General Election",
    sub="Quantile (Equal-Frequency) Class Intervals")
legend(locator(), legend=names(attr(colcode, "table")),
    fill=attr(colcode, "palette"), cex=0.6, bty="n")

No comments:

Post a Comment