Sunday, July 20, 2014

Where the Votes Are...

Political piece for this code is here.




require(plyr)
# jpeg_create()
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")

#df2014 <- read.delim("07.08.2014.txt",stringsAsFactors = FALSE) # Five year primary history 2009 - 2013
df2014 <- read.delim("05.20.2014.txt",stringsAsFactors = FALSE) # Five year general election history 2009 - 2013
DC <- subset(df2014,select=c(RegistrationNumber,Gender,ResidenceAddress,ResidenceCity,ResidenceState,ResidenceZipCode,PrecinctID,BallotCounted_1,BallotCounted_2,BallotCounted_3,BallotCounted_4,BallotCounted_5))
# DC_Sums <- data.frame(RegistrationNumber=DC$RegistrationNumber,Precinct=DC$PrecinctID,Sums=(rowSums(DC[,8:12],na.rm=TRUE)))

as.matrix(sort(with(DC,(table(ResidenceCity)))))
as.matrix(sort(with(DC,(table(ResidenceZipCode)))))

as.matrix(with(DC,(table(ResidenceCity,ResidenceZipCode))))
as.matrix(with(DC,(table(ResidenceZipCode,ResidenceCity))))

as.matrix.data.frame(with(DC,(table(ResidenceCity,ResidenceZipCode))))
as.matrix.data.frame(with(DC,(table(ResidenceZipCode,ResidenceCity))))

ftable(with(DC,(table(ResidenceCity,ResidenceZipCode))))
ftable(with(DC,(table(ResidenceZipCode,ResidenceCity))))

## 2012 and 2013 Elections # From the perspective of the 05.20.2014 or 07.08.2014, the NA simply weren't registered to vote
# > count(DC, "BallotCounted_1") # 2013 GE
#  BallotCounted_1  freq
#1               0 53080
#2               1 69012
#3              NA  4190
# > count(DC, "BallotCounted_2") # 2012 GE
#  BallotCounted_2  freq
#1               0 17044
#2               1 97212
#3              NA 12026

# Where BallotCounted_1 and BallotCounted_2 both equal 1 ('voted'): Tuple of Zip,City,Precinct produces ~ 240 divisions
DF1 <- subset(subset(data.frame(with(DC,(table(ResidenceZipCode,ResidenceCity,BallotCounted_1,PrecinctID)))),Freq != 0),BallotCounted_1 == 1)
DF2 <- subset(subset(data.frame(with(DC,(table(ResidenceZipCode,ResidenceCity,BallotCounted_2,PrecinctID)))),Freq != 0),BallotCounted_2 == 1)
arrange(DF1,desc(Freq))[1:10,]
arrange(DF2,desc(Freq))[1:10,]

# Where BallotCounted_1 and BallotCounted_2 both equal 1 ('voted'): By Precinct only ~ 178 precincts
DF1A <- subset(subset(data.frame(with(DC,(table(BallotCounted_1,PrecinctID)))),Freq != 0),BallotCounted_1 == 1)
DF2A <- subset(subset(data.frame(with(DC,(table(BallotCounted_2,PrecinctID)))),Freq != 0),BallotCounted_2 == 1)
arrange(DF1A,desc(Freq))[1:10,]
arrange(DF2A,desc(Freq))[1:10,]

# Where BallotCounted_1 and BallotCounted_2 both equal 0 ('not voted')
DF1B <- subset(subset(data.frame(with(DC,(table(BallotCounted_1,PrecinctID)))),Freq != 0),BallotCounted_1 == 0)
DF2B <- subset(subset(data.frame(with(DC,(table(BallotCounted_2,PrecinctID)))),Freq != 0),BallotCounted_2 == 0)
arrange(DF1B,desc(Freq))[1:10,]
arrange(DF2B,desc(Freq))[1:10,]

jpeg_create()
par(mfrow=c(1,1))
plot(DF1$Freq,col=rgb(1,0,0,.75),xlim=c(0,250),ylim=c(0,1000),cex=4.0,lwd=2,type="l",xlab="05/04/14 Active Voters WC GE 2012 (green), 2013 (red) : Split by unique City,Zip,Precinct Tuple",ylab="Voters")
mtext(sum(DF2A$Freq),side=1,line=1)
mtext(sum(DF1A$Freq),side=1,line=2)
par(new=t)
plot(DF2$Freq,col=rgb(0,1,0,.75),xlim=c(0,250),ylim=c(0,1000),cex=4.0,lwd=2,type="l",xlab="",ylab="")
par(new=F)

jpeg_create()
par(mfrow=c(1,1))
with(DF1A,plot(Freq,col=rgb(1,0,0,.75),xlim=c(0,177),ylim=c(0,1000),cex=4.0,lwd=2,type="l",xlab="05/04/14 Active Voters in WC GE 2012 (green), 2013 (red) by Precinct",ylab="Voters"))
mtext(sum(DF2A$Freq),side=1,line=1)
mtext(sum(DF1A$Freq),side=1,line=2)
par(new=t)
with(DF2A,plot(Freq,col=rgb(0,1,0,.75),xlim=c(0,177),ylim=c(0,1000),cex=4.0,lwd=2,type="l",xlab="",ylab="",yaxt="n"))
axis(3, 1:177,labels=DF1A$PrecinctID)
par(new=F)

jpeg_create()
par(mfrow=c(1,1))
with(DF1B,plot(Freq,col=rgb(1,0,0,.75),xlim=c(0,177),ylim=c(0,1000),cex=4.0,lwd=2,type="l",xlab="05/04/14 Active but not Voting in WC GE 2012 (green), 2013 (red) by Precinct",ylab="Voters"))
mtext(sum(DF1B$Freq),side=1,line=1)
mtext(sum(DF2B$Freq),side=1,line=2)
par(new=t)
with(DF2B,plot(Freq,col=rgb(0,1,0,.75),xlim=c(0,177),ylim=c(0,1000),cex=4.0,lwd=2,type="l",xlab="",ylab="",yaxt="n"))
axis(3, 1:177,labels=DF1A$PrecinctID)
par(new=F)

jpeg_create()
par(mfrow=c(1,1))
with(DF1A,plot(Freq,col=rgb(1,0,0,.75),xlim=c(0,177),ylim=c(0,1000),cex=4.0,lwd=2,type="l",xlab="",ylab="Voters"))
mtext("05/04/14 Active Voters in WC GE 2012 (green), 2013 (red) by Precinct",side=1,line=2)
par(new=t)
with(DF2A,plot(Freq,col=rgb(0,1,0,.75),xlim=c(0,177),ylim=c(0,1000),cex=4.0,lwd=2,type="l",xlab="",ylab="",yaxt="n"))
par(new=t)
with(DF1B,plot(Freq,col=rgb(1,0,1,.75),xlim=c(0,177),ylim=c(0,1000),cex=4.0,lwd=2,type="h",xlab="",ylab=""))
mtext("05/04/14 Active but not Voting in WC GE 2012 (grey), 2013 (magenta) by Precinct",side=1,line=3)
par(new=t)
with(DF2B,plot(Freq,col=rgb(.5,.5,.5,.75),xlim=c(0,177),ylim=c(0,1000),cex=4.0,lwd=2,type="h",xlab="",ylab="",yaxt="n"))
axis(3, 1:177,labels=DF1A$PrecinctID)
par(new=F)

jpeg_create()
par(mfrow=c(1,1))
with(DF1A,plot(Freq,col=rgb(1,0,0,.75),xlim=c(0,177),ylim=c(0,1000),cex=4.0,lwd=2,type="l",xlab="",ylab="Voters"))
mtext("05/04/14 Active Voters in WC GE 2013 (red) by Precinct",side=1,line=2)
par(new=t)
with(DF1B,plot(Freq,col=rgb(1,0,1,.75),xlim=c(0,177),ylim=c(0,1000),cex=4.0,lwd=2,type="h",xlab="",ylab="",yaxt="n"))
mtext("05/04/14 Active but not Voting in WC GE  2013 (magenta) by Precinct",side=1,line=3)
axis(3, 1:177,labels=DF1A$PrecinctID)
par(new=F)

jpeg_create()
par(mfrow=c(1,1))
with(DF2A,plot(Freq,col=rgb(0,1,0,.75),xlim=c(0,177),ylim=c(0,1000),cex=4.0,lwd=2,type="l",xlab="",ylab=""))
mtext("05/04/14 Active Voters in WC GE 2012 (green) by Precinct",side=1,line=2)
par(new=t)
with(DF2B,plot(Freq,col=rgb(.5,.5,.5,.75),xlim=c(0,177),ylim=c(0,1000),cex=4.0,lwd=2,type="h",xlab="",ylab="",yaxt="n"))
mtext("05/04/14 Active but not Voting in WC GE 2012 (grey) by Precinct",side=1,line=3)
axis(3, 1:177,labels=DF1A$PrecinctID)
par(new=F)

jpeg_create()
par(mfrow=c(1,2))
with(DF1A,plot(Freq,col=rgb(1,0,0,.75),xlim=c(0,177),ylim=c(0,1000),cex=4.0,lwd=2,type="l",xlab="",ylab="Voters"))
mtext("05/04/14 Active Voters in WC GE 2013 (red) by Precinct",side=1,line=2)
par(new=t)
with(DF1B,plot(Freq,col=rgb(1,0,1,.75),xlim=c(0,177),ylim=c(0,1000),cex=4.0,lwd=2,type="h",xlab="",ylab="",yaxt="n"))
mtext("05/04/14 Active but not Voting in WC GE  2013 (magenta) by Precinct",side=1,line=3)
axis(3, 1:177,labels=DF1A$PrecinctID)
par(new=F)

with(DF2A,plot(Freq,col=rgb(0,1,0,.75),xlim=c(0,177),ylim=c(0,1000),cex=4.0,lwd=2,type="l",xlab="",ylab=""))
mtext("05/04/14 Active Voters in WC GE 2012 (green) by Precinct",side=1,line=2)
par(new=t)
with(DF2B,plot(Freq,col=rgb(.5,.5,.5,.75),xlim=c(0,177),ylim=c(0,1000),cex=4.0,lwd=2,type="h",xlab="",ylab="",yaxt="n"))
mtext("05/04/14 Active but not Voting in WC GE 2012 (grey) by Precinct",side=1,line=3)
axis(3, 1:177,labels=DF1A$PrecinctID)
par(new=F)

jpeg_create()
par(mfrow=c(2,1))
with(DF1A,plot(Freq,col=rgb(1,0,0,.75),xlim=c(0,177),ylim=c(0,1000),cex=4.0,lwd=2,type="l",xlab="",ylab="Voters"))
mtext("05/04/14 Active Voters in WC GE 2013 (red) by Precinct",side=1,line=2)
par(new=t)
with(DF1B,plot(Freq,col=rgb(1,0,1,.75),xlim=c(0,177),ylim=c(0,1000),cex=4.0,lwd=2,type="h",xlab="",ylab="",yaxt="n"))
mtext("05/04/14 Active but not Voting in WC GE  2013 (magenta) by Precinct",side=1,line=3)
axis(3, 1:177,labels=DF1A$PrecinctID)
par(new=F)

with(DF2A,plot(Freq,col=rgb(0,1,0,.75),xlim=c(0,177),ylim=c(0,1000),cex=4.0,lwd=2,type="l",xlab="",ylab=""))
mtext("05/04/14 Active Voters in WC GE 2012 (green) by Precinct",side=1,line=2)
par(new=t)
with(DF2B,plot(Freq,col=rgb(.5,.5,.5,.75),xlim=c(0,177),ylim=c(0,1000),cex=4.0,lwd=2,type="h",xlab="",ylab="",yaxt="n"))
mtext("05/04/14 Active but not Voting in WC GE 2012 (grey) by Precinct",side=1,line=3)
axis(3, 1:177,labels=DF1A$PrecinctID)
par(new=F)

graphics.off()

No comments:

Post a Comment