Whatcom County 2015 GE Votes Good Per Precinct by quartile class interval. Click to Enlarge. More maps and political piece here |
# Code adapted from http://geog.uoregon.edu/bartlein/courses/geog495/old_index.html 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/WhatcomCountyPrecincts2015","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 2015 Consolidated
MB_2015_Consolidated <- read.delim("MB.11.24.15.FINAL.txt", header = TRUE, strip.white = TRUE, sep = "\t", quote = "", stringsAsFactors = FALSE)
MB_Consolidated_status <- subset(MB_2015_Consolidated, select = c(RegistrationNumber,PrecinctID,AVReturnStatus,AVReturnChallenge))
MB_2015_Consolidated_Good <- subset(MB_Consolidated_status, AVReturnStatus == "Good")
MB2015TO <- arrange(data.frame(with(MB_2015_Consolidated_Good,(table(PrecinctID)))),(PrecinctID))
### WC Data
colnames(MB2015TO) <- c("NAME", "VotesGood")
MB2015TO$NAME <- as.numeric(as.character(MB2015TO$NAME))
WCshp_merge_2015 <- sp::merge(WCshp,MB2015TO,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_2015 <- WCshp_merge_2015
## 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.8, bty="n")
## Check 2015
# merge(subset(dataProjected@data,select=c(NAME,VotesGood)),MB2015TO,by="NAME")
dataProjected <- dataProjected_2015
# 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 2015 Whatcom County General Election",
sub="Quantile (Equal-Frequency) Class Intervals")
legend(locator(), legend=names(attr(colcode, "table")),
fill=attr(colcode, "palette"), cex=0.8, 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(MB2015TO) <- c("PRECINCT_N", "VotesGood")
MB2015TO$PRECINCT_N <- as.numeric(as.character(MB2015TO$PRECINCT_N))
COBshp_merge_2015 <- sp::merge(COBshp,MB2015TO,by="PRECINCT_N")
dataProjected_2014 <- COBshp_merge_2014
dataProjected_2015 <- COBshp_merge_2015
## 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.8, bty="n")
## Check 2015
dataProjected <- dataProjected_2015
merge(subset(dataProjected@data,select=c(PRECINCT_N,VotesGood)),MB2015TO,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 2015 General Election",
sub="Quantile (Equal-Frequency) Class Intervals")
legend(locator(), legend=names(attr(colcode, "table")),
fill=attr(colcode, "palette"), cex=0.8, bty="n")
No comments:
Post a Comment