Political piece for this is here.
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")
df2014 <- read.delim("07.08.2014.txt",stringsAsFactors = FALSE)
DC <- subset(df2014,select=c(RegistrationNumber,PrecinctID,BallotCounted_1,BallotCounted_2,BallotCounted_3,BallotCounted_4,BallotCounted_5))
DC_all <- data.frame(RegistrationNumber=DC$RegistrationNumber,Precinct=DC$PrecinctID,Sums=(rowSums(DC[,3:7],na.rm=TRUE)))
as.matrix(with(DC_all,(table(Precinct))))
as.matrix(with(DC_all,(xtabs(Sums ~ Precinct))))
DC_extract_001 <- data.frame(V1=with(DC_all,(table(Precinct))),V2=with(DC_all,(xtabs(Sums ~ Precinct))))
DC_extract_002 <- data.frame(subset(DC_extract_001, select=c(V1.Precinct,V1.Freq,V2.Freq)),ratio1=DC_extract_001$V2.Freq / DC_extract_001$V1.Freq)
DC_extract_003 <- cbind(DC_extract_002,ratio2=DC_extract_002$V1.Freq * DC_extract_002$V2.Freq)
arrange(DC_extract_003,V1.Freq)
arrange(DC_extract_003,V2.Freq)
arrange(DC_extract_003,ratio1)
arrange(DC_extract_003,ratio2)
write.csv(arrange(DC_extract_003,V1.Freq),"Freq1.csv")
write.csv(arrange(DC_extract_003,V2.Freq),"Freq2.csv")
write.csv(arrange(DC_extract_003,ratio1),"Ratio1.csv")
write.csv(arrange(DC_extract_003,ratio2),"Ratio2.csv")
jpeg_create()
par(mfrow=c(1,2))
with(DC_extract_003,barplot(V1.Freq,names.arg=V1.Precinct,col="red",xlab="Precincts",ylab="Registrants"))
with(DC_extract_003,barplot(V2.Freq,names.arg=V1.Precinct,col="blue",xlab="Precincts",ylab="Cumulative Years Voted"))
jpeg_create()
par(mfrow=c(1,2))
with(DC_extract_003,barplot(ratio1,names.arg=V1.Precinct,col=rgb(1,0,0,.75),xlab="Precincts",ylab="2009 - 2013 Primaries: Cumulative Years Voted / Registrants"))
with(DC_extract_003,barplot(ratio2,names.arg=V1.Precinct,col=rgb(0,0,1,.75),xlab="Precincts",ylab="2009 - 2013 Primaries: Cumulative Years Voted * Registrants"))
jpeg_create()
par(mfrow=c(1,1))
with(DC_extract_003,barplot(ratio1,names.arg=V1.Precinct,col=rgb(1,0,0,.75),xlab="Precincts"))
par(new=t)
with(DC_extract_003,barplot(ratio2,names.arg=V1.Precinct,col=rgb(0,0,1,.75),xlab="Precincts",yaxt="n"))
par(new=F)
axis(4)
mtext("Red (left) : 2009 - 2013 Primaries: Cumulative Years Voted / Registrants", side = 3, line = 2)
mtext("Blue/Purple (right) : 2009 - 2013 Primaries: Cumulative Years Voted * Registrants", side = 3, line = 1)
jpeg_create()
par(mfrow=c(4,1))
with(DC_extract_003,barplot(V1.Freq,names.arg=V1.Precinct,col="red",xlab="Precincts",ylab="Registrants"))
mtext("2009 - 2013 Primaries:", side = 3, line = 0)
with(DC_extract_003,barplot(V2.Freq,names.arg=V1.Precinct,col="blue",xlab="Precincts",ylab="Cumulative Years Voted"))
with(DC_extract_003,barplot(ratio1,names.arg=V1.Precinct,col=rgb(1,0,0,.75),xlab="Precincts",ylab="Cumulative Years Voted / Registrants"))
with(DC_extract_003,barplot(ratio2,names.arg=V1.Precinct,col=rgb(0,0,1,.75),xlab="Precincts",ylab="Cumulative Years Voted * Registrants"))
jpeg_create()
par(mfrow=c(4,1))
barplot(arrange(DC_extract_003,V1.Freq)[,2],names.arg=arrange(DC_extract_003,V1.Freq)[,1],col="red",xlab="Precincts",ylab="Registrants")
mtext("2009 - 2013 Primaries:", side = 3, line = 0)
barplot(arrange(DC_extract_003,V2.Freq)[,3],names.arg=arrange(DC_extract_003,V2.Freq)[,1],col="blue",xlab="Precincts",ylab="Cumulative Years Voted")
barplot(arrange(DC_extract_003,ratio1)[,4],names.arg=arrange(DC_extract_003,ratio1)[,1],col=rgb(1,0,0,.75),xlab="Precincts",ylab="Cumulative Years Voted / Registrants")
barplot(arrange(DC_extract_003,ratio2)[,5],names.arg=arrange(DC_extract_003,ratio2)[,1],col=rgb(0,0,1,.75),xlab="Precincts",ylab="Cumulative Years Voted * Registrants")
graphics.off()
par(mfrow=c(1,1))
plot(DC_extract_003$ratio1,cex=1,col="red",type="b")
par(new=t)
plot(DC_extract_003$ratio2,cex=1,col="blue",type="b")
par(new=F)
par(mfrow=c(1,2))
plot(DC_extract_003$ratio1 ~ DC_extract_003$V1.Precinct)
plot(DC_extract_003$ratio2 ~ DC_extract_003$V1.Precinct)
par(mfrow=c(1,2))
plot(DC_extract_003$ratio1,cex=1,col="red",type="b")
plot(DC_extract_003$ratio2,cex=1,col="blue",type="b")
par(mfrow=c(1,2))
plot(DC_extract_003$ratio1 ~ DC_extract_003$V1.Precinct,cex=1,col="red",type="b")
plot(DC_extract_003$ratio2 ~ DC_extract_003$V1.Precinct,cex=1,col="blue",type="b")
par(mfrow=c(1,1))
plot(DC_extract_003$ratio1,cex=1,col="red",type="b",ylab=" ",xlab="Precincts")
par(new=t)
plot(DC_extract_003$ratio2,cex=1,col="blue",type="b",ylab=" ",xlab="")
par(new=F)
par(mfrow=c(2,1))
barplot(sort(DC_extract_003[150:177,]$V1.Freq),col=rgb(1,0,0,.75),ylim=c(0,1200),xlab=sum(DC_extract_003[150:177,]$V1.Freq) - sum(DC_extract_003[122:149,]$V1.Freq))
par(new=t)
barplot(sort(DC_extract_003[122:149,]$V1.Freq),col=rgb(0,0,1,.75),ylim=c(0,1200))
mtext("07/08/2014 Active Voters: 300:800 series (red=27) vs 241:268 series (blue=27) Precincts", side = 3, line = 2)
mtext("(300:800) series - (241:268); Sum and Difference on X axis label", side = 3, line = 1)
mtext(sum(DC_extract_003[150:177,]$V1.Freq) + sum(DC_extract_003[122:149,]$V1.Freq),side = 1, line = 2)
par(new=F)
barplot(sort(DC_extract_003[1:81,]$V1.Freq),col=rgb(1,0,0,.75),ylim=c(0,1200),xlab=sum(DC_extract_003[1:81,]$V1.Freq) - sum(DC_extract_003[82:149,]$V1.Freq))
par(new=t)
barplot(sort(DC_extract_003[82:149,]$V1.Freq),col=rgb(0,0,1,.75),ylim=c(0,1200))
mtext("07/08/2014 Active Voters: 100 series (red=81) vs 200 series (blue=67) Precincts", side = 3, line = 2)
mtext("100 series - 200 series; Sum and Difference on X axis label", side = 3, line = 1)
mtext(sum(DC_extract_003[1:81,]$V1.Freq) + sum(DC_extract_003[82:149,]$V1.Freq),side = 1, line = 2)
par(new=F)
# WC42 <- DC_extract_003[c(c(1:57),c(150:177)),]
# COB42 <- DC_extract_003[c(c(82:106),c(107,110,114,148,149)),]
par(mfrow=c(1,1))
barplot(sort(DC_extract_003[c(c(1:57),c(150:177)),]$V1.Freq),col=rgb(1,0,0,.75),ylim=c(0,1200),xlab=sum(DC_extract_003[c(c(1:57),c(150:177)),]$V1.Freq) - sum(DC_extract_003[c(c(82:106),c(107,110,114,148,149)),]$V1.Freq))
par(new=t)
barplot(sort(DC_extract_003[c(c(82:106),c(107,110,114,148,149)),]$V1.Freq),col=rgb(0,0,1,.75),ylim=c(0,1200))
mtext("07/08/2014 Active Voters: 42nd Precinct", side = 3, line = 2)
mtext("100 + 300:800 series (red=85) vs 200 series (blue=30) Precincts", side = 3, line = 1)
mtext("100 + 300:800 series - 200; Sum and Difference on X axis label", side = 3, line = 0)
mtext(sum(DC_extract_003[c(c(1:57),c(150:177)),]$V1.Freq) + sum(DC_extract_003[c(c(82:106),c(107,110,114,148,149)),]$V1.Freq), side = 1, line = 2)
par(new=F)
No comments:
Post a Comment