Monday, July 14, 2014

How RED or BLUE?

The political post for this code is here.


# http://vote.wa.gov/results/20131105/export/20131105_WhatcomPrecincts_20131210_0314.csv
# Before importing, I edit this file in Notepad++
# The file format is all races WA so subsetting and xtabs are required to parse.
# Add headers (Prop_DESC,Prop_NAME,Precinct1,Precinct2,Votes)
# Also remove excess tabs

require(plyr)
jpeg_create <- function() {
 systime <- as.numeric(Sys.time())
 # dev.new()
 jpeg(filename = systime,
          width = 1024, height = 768, units = "px", pointsize = 12,
          quality = 100, bg = "white", res = NA, family = "", restoreConsole = TRUE,
          type = c("windows"))
 Sys.sleep(2)
   }
setwd("C:/Politics")

WCGEN2013 <- read.csv("20131105_WhatcomPrecincts_20131210_0314_edit.csv",stringsAsFactors = FALSE)
WCGEN2013 <- subset(WCGEN2013, Precinct1 !="Total")
names(WCGEN2013)
uniqPrecinctID <- unique(WCGEN2013$Precinct1)
uniqPrecinctID <- data.frame(PrecinctID=sort(uniqPrecinctID[1:178]))
AllVotes <- data.frame(with(WCGEN2013,(xtabs(Votes ~ Precinct2))))

uniqWCGEN2013 <- unique(WCGEN2013$Prop_DESC)
# WCGEN2013$Prop_NAME <- as.factor(WCGEN2013$Prop_NAME)
# WCGEN2013$Precinct1 <- as.numeric(WCGEN2013$Precinct1)
# WCGEN2013$Precinct2 <- as.numeric(WCGEN2013$Precinct2)
# WCGEN2013$Votes <- as.numeric(WCGEN2013$Votes)
# WCCD1A <- WCCD1A[(1 : nrow(WCCD1A) - 1),]
# WCCD1A <- 

WCCD1A <- subset(WCGEN2013, Prop_DESC == "Whatcom County Council District 1 Position A")
data.frame(with(WCCD1A,(xtabs(Votes ~ Precinct1))))
data.frame(with(WCCD1A,(xtabs(Votes ~ Prop_NAME))))
as.matrix(with(WCCD1A,(xtabs(Votes ~ Precinct1  + Prop_NAME))))

WCCD2A <- subset(WCGEN2013, Prop_DESC == "Whatcom County Council District 2 Position A")
data.frame(with(WCCD2A,(xtabs(Votes ~ Precinct1))))
data.frame(with(WCCD2A,(xtabs(Votes ~ Prop_NAME))))
as.matrix(with(WCCD2A,(xtabs(Votes ~ Precinct1  + Prop_NAME))))

WCCD3A <- subset(WCGEN2013, Prop_DESC == "Whatcom County Council District 3 Position A")
data.frame(with(WCCD3A,(xtabs(Votes ~ Precinct1))))
data.frame(with(WCCD3A,(xtabs(Votes ~ Prop_NAME))))
as.matrix(with(WCCD3A,(xtabs(Votes ~ Precinct1  + Prop_NAME))))

# (as.matrix(with(WCCD1A,(xtabs(Votes ~ Precinct1  + Prop_NAME)))))
# as.matrix.data.frame(with(WCCD1A,(xtabs(Votes ~ Precinct1  + Prop_NAME))))
# as.matrix.data.frame(with(WCCD1A,(xtabs(Votes ~ Precinct1  + Prop_NAME))))
# data.frame(as.matrix.data.frame(with(WCCD1A,(xtabs(Votes ~ Precinct1  + Prop_NAME)))))

D1A <- data.frame(with(WCCD1A,(xtabs(Votes ~ Precinct1  + Prop_NAME))))
D2A <- data.frame(with(WCCD2A,(xtabs(Votes ~ Precinct1  + Prop_NAME))))
D3A <- data.frame(with(WCCD3A,(xtabs(Votes ~ Precinct1  + Prop_NAME))))
D1 <- data.frame(rbind(D1A,D2A,D3A))

as.matrix(with(D1,(xtabs(Freq ~ Precinct1  + Prop_NAME))))
D2 <- data.frame(as.matrix.data.frame(with(D1,(xtabs(Freq ~ Precinct1  + Prop_NAME)))))
# Barry Buchanan Kathy Kershner Ben Elenbaas Ken Mann Carl Weimer Michelle Luke
D3 <- data.frame(PrecinctID=uniqPrecinctID$PrecinctID,BB=D2$X1,KK=D2$X2,BE=D2$X3,KM=D2$X4,CW=D2$X5,ML=D2$X6)
D4 <- cbind(D3,ALL=rowSums(D3[,2:7],na.rm=TRUE),BLUE=rowSums(D3[,c(2,5,6)],na.rm=TRUE),RED=rowSums(D3[,c(3,4,7)],na.rm=TRUE))
D5 <- cbind(D4, BOA=D4$BLUE/D4$ALL, ROA=D4$RED/D4$ALL)
D6 <- cbind(D3,ALL=rowSums(D3[,2:7],na.rm=TRUE))
D7 <- data.frame(D6, RED1=D6[3]/D6$ALL,BLUE1=D6[2]/D6$ALL,RED1A=D6[3]/(D6[3] + D6[2]),BLUE1A=D6[2]/(D6[3] + D6[2]),RED2=D6[4]/D6$ALL,BLUE2=D6[5]/D6$ALL,RED2A=D6[4]/(D6[5] + D6[4]),BLUE2A=D6[5]/(D6[5] + D6[4]),RED3=D6[7]/D6$ALL,BLUE3=D6[6]/D6$ALL,RED3A=D6[7]/(D6[7] + D6[6]),BLUE3A=D6[6]/(D6[7] + D6[6]))

jpeg_create()
barplot(D5$BOA,names.arg=D5$PrecinctID,col=rgb(1,0,0,.75))
mtext("2013 General Election Results for WC Council D1A,D2A,D3A", side=3,line=2)
mtext("Blue lines (left) are percentage of Democrat votes.", side=3,line=1)
mtext("Red lines (right) are total Democrat votes", side=3,line=0)
mtext("Precincts",side=1,line=2)
par(new=t)
barplot(D5$BLUE,names.arg=D5$PrecinctID,col=rgb(0,0,1,.75),yaxt="n")
par(new=F)
axis(4)

jpeg_create()
barplot(D5$ROA,names.arg=D5$PrecinctID,col=rgb(1,0,0,.75))
mtext("2013 General Election Results for WC Council D1A,D2A,D3A", side=3,line=2)
mtext("Blue lines (left) are percentage of Republican votes.", side=3,line=1)
mtext("Red lines (right) are total Republican votes", side=3,line=0)
mtext("Precincts",side=1,line=2)
par(new=t)
barplot(D5$RED,names.arg=D5$PrecinctID,col=rgb(0,0,1,.75),yaxt="n")
par(new=F)
axis(4)

jpeg_create()
barplot((D5$BOA *D5$BLUE),names.arg=D5$PrecinctID,col=rgb(0,0,1,.75))
mtext("2013 General Election Results for WC Council D1A,D2A,D3A", side=3,line=2)
mtext("Cumulative percentage of Democrat votes * Democrat Votes for WC Council 1A,2A,3A.", side=3,line=1)
# mtext("D5$BOA *D5$BLUE", side=3,line=0)
mtext("Precincts",side=1,line=2)

jpeg_create()
barplot((D5$ROA * D5$RED),names.arg=D5$PrecinctID,col=rgb(1,0,0,.75))
mtext("2013 General Election Results for WC Council D1A,D2A,D3A", side=3,line=2)
mtext("Cumulative percentage of Republican votes * Republican Votes for WC Council 1A,2A,3A.", side=3,line=1)
# mtext("D5$ROA * D5$RED", side=3,line=0)
mtext("Precincts",side=1,line=2)

jpeg_create()
barplot((D5$BOA * D5$BLUE),names.arg=D5$PrecinctID,col=rgb(0,0,1,.75),ylim=c(0,1400))
mtext("2013 General Election Results for WC Council D1A,D2A,D3A", side=3,line=2)
mtext("Blue: Democrat votes.", side=3,line=1)
mtext("Red: Republican votes", side=3,line=0)
mtext("Precincts",side=1,line=2)
par(new=t)
barplot((D5$ROA * D5$RED),names.arg=D5$PrecinctID,col=rgb(1,0,0,.75),ylim=c(0,1400))
par(new=F)

jpeg_create()
par(mfrow=c(1,2))
X_blue <- as.vector(arrange(D5,desc(BOA))[1:30,11])
Y_blue <- as.vector(arrange(D5,desc(BOA))[1:30,1])
X <- X_blue
Y < Y_blue
barplot(X, names.arg=Y,col=rgb(0,0,1,.75),ylim=c(0,1))
#mtext("2013 General Election Results for WC Council D1A,D2A,D3A", side=3,line=2)
mtext("GE 2013: Top 30 Democrat (BLUE) Precincts", side=3,line=1)
X_red <- as.vector(arrange(D5,desc(ROA))[1:30,12])
Y_red <- as.vector(arrange(D5,desc(ROA))[1:30,1])
X <- X_red
Y <- Y_red
barplot(X, names.arg=Y,col=rgb(1,0,0,.75),ylim=c(0,1))
#mtext("2013 General Election Results for WC Council D1A,D2A,D3A", side=3,line=2)
mtext("GE 2013: Top 30 Republican (RED) Precincts", side=3,line=1)

graphics.off()

data.frame(cbind(cbind(PrecinctBLUE=Y_blue,PercentageBLUE=X_blue),cbind(PrecinctRED=Y_red,PercentageRED=X_red)))

No comments:

Post a Comment