Monday, February 8, 2016

VoterDensity_000.r

# VoterDensity_000.r 
# Needs .GlobalEnv derived from VoterDensity.r


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

attach(a1_LG1_LG2)
x <- Louws + Gilfilen
y <- WC_1y + WC_1n
z <- PID
par(mfrow=c(4,1))
barplot(y, names.arg=z,cex.names=.5,las=2,xlab="Jail(white)" ,col=rgb(1,0,0))
barplot(x, names.arg=z,cex.names=.5,las=2,xlab="Executive(grey)",col=rgb(.5,.5,.5))
barplot((x/y) * 100, names.arg=z,cex.names=.5,las=2,xlab="Executive/Jail(blue)" ,ylim=c(0,100),col=rgb(0,0,1,.5))
plot((y),col=rgb(1,0,1),type="l",xlab="")
lines((x),col=rgb(.5,.5,.5))
lines(((x/y) * 1000),col=rgb(0,0,1,.5))
par(mfrow=c(1,1))
detach(a1_LG1_LG2)


jpeg_create()
attach(a1_LG1_LG2)
par(mfrow=c(1,1))
w <- Total_Registered - Good
x <- Louws + Gilfilen
y <- WC_1y + WC_1n
z <- PID
plot(y ~ z,cex=1,pch=2,las=2,xlab="Precincts",ylab="Votes" ,col=rgb(1,0,0),type="p")
points(x ~ z, cex=1.5,las=2,xlab="",col=rgb(.5,.5,.5))
lines(lowess(Louws ~ z) ,lwd=1.5,col=rgb(.75,0,0))
lines(lowess(Gilfilen ~ z) ,lwd=1.5,col=rgb(0,0,.75))
lines(lowess(WC_1n ~ z) ,lwd=1.5,col=rgb(1,1,0))
lines(lowess(WC_1y ~ z) ,lwd=1.5,col=rgb(0,1,1))
lines(lowess(w ~ z),lwd=1.5,col=rgb(1,0,1))
mtext("Contest Votes by Precinct for GE 2015. (All) Votes for Jail(red triangles) vs. All Votes for Executive(grey circles).")
mtext("Lowess smoothing: Jail Yes in Aquamarine , Jail No in Yellow, Louws in Red, Gilfilen in Blue, (Registered - Good) in Purple." ,line=-1)
mtext(paste0("(Louws - Gilfilen) = ",sum(Louws - Gilfilen)),line=-2)
mtext(paste0("(Jail No - Jail Yes) = ",sum(WC_1n - WC_1y)),line=-3)
mtext(paste0("(Total_Registered - Good) = ",sum(w)),line=-4)
mtext(paste0("(Good) = ",sum(Good)),line=-5)
for(i in PID) {abline(v=i,lwd=.25,col=rgb(.5,.5,.5,.25))}
abline(h=0)
detach(a1_LG1_LG2)

jpeg_create()
h1 <- arrange(subset(a1_LG1_LG2,PID >= 200 & PID <= 299))
attach(h1)
par(mfrow=c(1,1))
w <- Total_Registered - Good
x <- Louws + Gilfilen
y <- WC_1y + WC_1n
z <- PID
plot(y ~ z,cex=1,pch=2,las=2,xlab="Precincts",ylab="Votes" ,col=rgb(1,0,0),type="p")
points(x ~ z, cex=1.5,las=2,xlab="",col=rgb(.5,.5,.5))
lines(lowess(Louws ~ z) ,lwd=1.5,col=rgb(.75,0,0))
lines(lowess(Gilfilen ~ z) ,lwd=1.5,col=rgb(0,0,.75))
lines(lowess(WC_1n ~ z) ,lwd=1.5,col=rgb(1,1,0))
lines(lowess(WC_1y ~ z) ,lwd=1.5,col=rgb(0,1,1))
lines(lowess(w ~ z),lwd=1.5,col=rgb(1,0,1))
mtext("Contest Votes by Precinct for GE 2015. (All) Votes for Jail(red triangles) vs. All Votes for Executive(grey circles).")
mtext("Lowess smoothing: Jail Yes in Aquamarine , Jail No in Yellow, Louws in Red, Gilfilen in Blue, (Registered - Good) in Purple." ,line=-1)
mtext(paste0("(Louws - Gilfilen) = ",sum(Louws - Gilfilen)),line=-2)
mtext(paste0("(Jail No - Jail Yes) = ",sum(WC_1n - WC_1y)),line=-3)
mtext(paste0("(Total_Registered - Good) = ",sum(w)),line=-4)
mtext(paste0("(Good) = ",sum(Good)),line=-5)
for(i in PID) {abline(v=i,lwd=.25,col=rgb(.5,.5,.5,.25))}
abline(h=0)
detach(h1)

jpeg_create()
h2 <- arrange(subset(a1_LG1_LG2,PID >= 100 & PID <= 199))
attach(h2)
par(mfrow=c(1,1))
w <- Total_Registered - Good
x <- Louws + Gilfilen
y <- WC_1y + WC_1n
z <- PID
plot(y ~ z,cex=1,pch=2,las=2,xlab="Precincts",ylab="Votes" ,col=rgb(1,0,0),type="p")
points(x ~ z, cex=1.5,las=2,xlab="",col=rgb(.5,.5,.5))
lines(lowess(Louws ~ z) ,lwd=1.5,col=rgb(.75,0,0))
lines(lowess(Gilfilen ~ z) ,lwd=1.5,col=rgb(0,0,.75))
lines(lowess(WC_1n ~ z) ,lwd=1.5,col=rgb(1,1,0))
lines(lowess(WC_1y ~ z) ,lwd=1.5,col=rgb(0,1,1))
lines(lowess(w ~ z),lwd=1.5,col=rgb(1,0,1))
mtext("Contest Votes by Precinct for GE 2015. (All) Votes for Jail(red triangles) vs. All Votes for Executive(grey circles).")
mtext("Lowess smoothing: Jail Yes in Aquamarine , Jail No in Yellow, Louws in Red, Gilfilen in Blue, (Registered - Good) in Purple." ,line=-1)
mtext(paste0("(Louws - Gilfilen) = ",sum(Louws - Gilfilen)),line=-2)
mtext(paste0("(Jail No - Jail Yes) = ",sum(WC_1n - WC_1y)),line=-3)
mtext(paste0("(Total_Registered - Good) = ",sum(w)),line=-4)
mtext(paste0("(Good) = ",sum(Good)),line=-5)
for(i in PID) {abline(v=i,lwd=.25,col=rgb(.5,.5,.5,.25))}
abline(h=0)
detach(h2)

jpeg_create()
attach(a1_LG1_LG2)
par(mfrow=c(1,1))
w <- Total_Registered - Good
x <- Louws + Gilfilen
y <- WC_1y + WC_1n
z <- PID
plot(y ~ z,cex=1,pch=2,las=2,xlab="Precincts",ylab="Votes" ,col=rgb(1,0,0),type="p")
points(x ~ z, cex=1.5,las=2,xlab="",col=rgb(.5,.5,.5))
points((y - x) ~ z,pch=19,cex=1.5,las=2,xlab="",col=rgb(0,1,0,.15))
lines(lowess((y - x) ~ z),lwd=1.5,col=rgb(0,1,0))
lines(lowess(w ~ z),lwd=1.5,col=rgb(1,0,1))
lines(lowess(y ~ z),lwd=1.5,col=rgb(.75,0,0))
lines(lowess(x ~ z) ,lwd=1.5,col=rgb(0,0,.75))
par(mfrow=c(1,1))
mtext("Determining 'Falloff': Contest Votes by Precinct for GE 2015. (All) Votes for Jail(red triangles) vs. All Votes for Executive(grey circles).")
mtext("Lowess smoothing: Jail in Red, Executive in blue, Falloff Diff (Jail - Executive) in Green, (Registered - Good) in Purple." ,line=-1)
mtext("Falloff Difference: (Jail - Executive) per Precinct in green." ,line=-2)
mtext("Total Not Voted  (Total_Registered - Good) = ",line=-3)
mtext(sum(w),line=-4)
mtext("Total Falloff Difference (Jail - Executive) = ",line=-5)
mtext(sum(y - x),line=-6)
for(i in PID) {abline(v=i,lwd=.25,col=rgb(.5,.5,.5,.25))}
abline(h=0)
detach(a1_LG1_LG2)

jpeg_create()
h1 <- arrange(subset(a1_LG1_LG2,PID >= 200 & PID <= 299))
attach(h1)
par(mfrow=c(1,1))
w <- Total_Registered - Good
x <- Louws + Gilfilen
y <- WC_1y + WC_1n
z <- PID
plot(y ~ z,cex=1,pch=2,las=2,xlab="Precincts",ylab="Votes" ,col=rgb(1,0,0),type="p")
points(x ~ z, cex=1.5,las=2,xlab="",col=rgb(.5,.5,.5))
points((y - x) ~ z,pch=19,cex=1.5,las=2,xlab="",col=rgb(0,1,0,.15))
lines(lowess((y - x) ~ z),lwd=1.5,col=rgb(0,1,0))
lines(lowess(w ~ z),lwd=1.5,col=rgb(1,0,1))
lines(lowess(y ~ z),lwd=1.5,col=rgb(.75,0,0))
lines(lowess(x ~ z) ,lwd=1.5,col=rgb(0,0,.75))
par(mfrow=c(1,1))
mtext("Determining 'Falloff': Contest Votes by Precinct for GE 2015. (All) Votes for Jail(red triangles) vs. All Votes for Executive(grey circles).")
mtext("Lowess smoothing: Jail in Red, Executive in blue, Falloff Diff (Jail - Executive) in Green, (Registered - Good) in Purple." ,line=-1)
mtext("Falloff Difference: (Jail - Executive) per Precinct in green." ,line=-2)
mtext("Total Not Voted  (Total_Registered - Good) = ",line=-3)
mtext(sum(w),line=-4)
mtext("Total Falloff Difference (Jail - Executive) = ",line=-5)
mtext(sum(y - x),line=-6)
for(i in PID) {abline(v=i,lwd=.25,col=rgb(.5,.5,.5,.25))}
abline(h=0)
detach(h1)

jpeg_create()
h2 <- arrange(subset(a1_LG1_LG2,PID >= 100 & PID <= 199))
attach(h2)
par(mfrow=c(1,1))
w <- Total_Registered - Good
x <- Louws + Gilfilen
y <- WC_1y + WC_1n
z <- PID
plot(y ~ z,cex=1,pch=2,las=2,xlab="Precincts",ylab="Votes" ,col=rgb(1,0,0),type="p")
points(x ~ z, cex=1.5,las=2,xlab="",col=rgb(.5,.5,.5))
points((y - x) ~ z,pch=19,cex=1.5,las=2,xlab="",col=rgb(0,1,0,.15))
lines(lowess((y - x) ~ z),lwd=1.5,col=rgb(0,1,0))
lines(lowess(w ~ z),lwd=1.5,col=rgb(1,0,1))
lines(lowess(y ~ z),lwd=1.5,col=rgb(.75,0,0))
lines(lowess(x ~ z) ,lwd=1.5,col=rgb(0,0,.75))
par(mfrow=c(1,1))
mtext("Determining 'Falloff': Contest Votes by Precinct for GE 2015. (All) Votes for Jail(red triangles) vs. All Votes for Executive(grey circles).")
mtext("Lowess smoothing: Jail in Red, Executive in blue, Falloff Diff (Jail - Executive) in Green, (Registered - Good) in Purple." ,line=-1)
mtext("Falloff Difference: (Jail - Executive) per Precinct in green." ,line=-2)
mtext("Total Not Voted  (Total_Registered - Good) = ",line=-3)
mtext(sum(w),line=-4)
mtext("Total Falloff Difference (Jail - Executive) = ",line=-5)
mtext(sum(y - x),line=-6)
for(i in PID) {abline(v=i,lwd=.25,col=rgb(.5,.5,.5,.25))}
abline(h=0)
detach(h2)
graphics.off()

# lines((((x/y) * 100)) ~ z, lwd=2,col=rgb(0,0,.75))
# lines(lowess(((x/y) * 100) ~ z) , lwd=2,col=rgb(.75,0,0))

# 3D plotting
library(rgl)
attach(a1_LG1_LG2)
x <- Louws
y <- Gilfilen
z <- {}
# xyz.coords(x, y, z = NULL)
plot3d(x, y, z, "Jack(red)","Joy(blue)","Precincts(Z axis)",size=1.25,type="s",col = c("red","blue"))
detach(a1_LG1_LG2)

library(rgl)
attach(a1_LG1_LG2)
x <- Louws
y <- Gilfilen
z <- {}
# xyz.coords(x, y, z = NULL)
plot3d(x, y, z, "Jack(red)","Joy(blue)","Precincts(Z axis)",size=1.25,type="s",col = c(rgb(1,0,0,.25),rgb(0,0,1,.25)),
main="Jack and Joy per Precinct 2015 GE")
detach(a1_LG1_LG2)

library(rgl)
attach(a1_LG1_LG2)
x <- Louws + Gilfilen
y <- WC_1y + WC_1n
z <- {}
# xyz.coords(x, y, z = NULL)
plot3d(x, y, z, "Jack + Joy(red)","Jail(blue)","Precincts(Z axis)",size=1.25,type="s",col = c(rgb(1,0,0,.25),rgb(0,0,1,.25)),
main="Executive + Jail votes per Precinct 2015 GE")
detach(a1_LG1_LG2)

library(rgl)
attach(a1_LG1_LG2)
x <- Good
y <- Total_Registered
z <- {}
# xyz.coords(x, y, z = NULL)
plot3d(x, y, z, "Votes Good(black)","Registered(grey)","Precincts(Z axis)",size=1.25,type="s",col = c(rgb(1,1,1,.5),rgb(.5,.5,.5,.5)),
main="Votes counted Good and Registrations per Precinct 2015 GE")
detach(a1_LG1_LG2)

attach(a1_LG1_LG2)
x <- Good
y <- Total_Registered
z <- PID
par(mfrow=c(4,1))
barplot(y, names.arg=z,cex.names=.5,las=2,xlab="Registered(white)" ,ylim=c(0,1400),col=rgb(1,1,1))
barplot(x, names.arg=z,cex.names=.5,las=2,xlab="Votes Good(grey)",ylim=c(0,650),col=rgb(.5,.5,.5))
barplot((x/y) * 100, names.arg=z,cex.names=.5,las=2,xlab="Good/Registered(blue)" ,ylim=c(0,100),col=rgb(0,0,1,.5))
plot((y),col=rgb(1,0,1),type="l",xlab="")
lines((x),col=rgb(.5,.5,.5))
lines(((x/y) * 1000),col=rgb(0,0,1,.5))
par(mfrow=c(1,1))
detach(a1_LG1_LG2)

attach(a1_LG1_LG2)
x <- Good
y <- Total_Registered
barplot(x, names.arg=z,cex.names=.75,las=2,xlab="",col = rgb(1,0,0,.25))
par(new=FALSE)
barplot(y, names.arg=z,cex.names=.75,las=2,ylab="",col = rgb(0,0,1,.25))
par(new=TRUE)
mtext("Votes counted Good and Registrations per Precinct 2015 GE")
detach(a1_LG1_LG2)

No comments:

Post a Comment