Monday, February 8, 2016

VoterDensity_002.r

# VoterDensity_002.r 
# Needs .GlobalEnv derived from VoterDensity.r
# 3D charts with rgl and plot3D


library(rgl)
library(plot3D)
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)
   }
 

m1 <- as.matrix(a1_LG1_LG2)
m3 <- m1[,c(8,9)]

# jpeg_create()
par(mfrow=c(1,3))
hist3D(x =1:177, y = 1:2, z = m3,cex= 4,
         bty = "g", phi = 20,  theta = -60,
         xlab = "177 Precincts: 101-801", ylab = "votes", zlab = "",
main = "Matrix1: Jail No vs. Jail Yes",
         zlim=c(0,max(m3)),col = rainbow(177), border = "black", shade = 0.8,
         ticktype = "detailed", space = 0.15, d = 2, cex.axis = 1e-9)
mtext(paste0("Jail_No = ",sum(m3[,2])),col=rgb(0,0,1,.5),line=-1)
mtext(paste0("Jail_Yes = ",sum(m3[,1])),col=rgb(1,0,0,.5),line=-2)
 
# plotrgl()
 
m2 <- m1[,c(6,7)]
hist3D(x =1:177, y = 1:2, z = m2,cex= 4,
         bty = "g", phi = 20,  theta = -60,
         xlab = "177 Precincts: 101-801", ylab = "Votes", zlab = "",
main = "Matrix2: Gilfilen vs. Louws",
         zlim=c(min(m2),max(m2)),col = rainbow(177), border = "black", shade = 0.8,
         ticktype = "detailed", space = 0.15, d = 2, cex.axis = 1e-9)
mtext(paste0("Gilfilen = ",sum(m2[,2])),col=rgb(0,0,1,.5),line=-1)
mtext(paste0("Louws = ",sum(m2[,1])),col=rgb(1,0,0,.5),line=-2)
# plotrgl()

m4 <- (m3 - m2);colnames(m4) <- c("JackLoss","JoyLoss")
hist3D(x =1:177, y = 1:2, z = m4,cex= 4,
         bty = "g", phi = 20,  theta = -60,
         xlab = "177 Precincts: 101-801", ylab = "Jail_No - Gilfilen; Jail_Yes - Louws", zlab = "",
main = "Votes Lost(?) Matrix1 (Jail) - Matrix2 (Executive)",
         zlim=c(min(m4),max(m4)),col = rainbow(177), border = "black", shade = 0.8,
         ticktype = "detailed", space = 0.15, d = 2,cex= 2, cex.axis = 1e-9)
mtext(paste0("Jail_No - Gilfilen = ",sum(m4[,2])),col=rgb(0,0,1,.5),line=-1)
mtext(paste0("Jail_Yes - Louws = ",sum(m4[,1])),col=rgb(1,0,0,.5),line=-2)
# text(locator(),cex= 2,label="0",col="red",lwd=5)
# text(locator(),cex= 2,label="0",col="red",lwd=5)
   # lines(locator(),cex= 2,col="red",lwd=5)
# lines(locator(),cex= 2,col="red",lwd=5)
# plotrgl()
par(mfrow=c(1,1))
mtext("The 'plot3D' library allows us to look at precinct histograms as if precinct totals were elevations.",col="blue",cex=1.5,side=1,line= -2)

plot(m4[,1],cex=1.25,pch=19,xlim=c(1,177),xlab="Precincts",las=2)
for(i in m4[,1]){abline(h=i,col=rgb(0,0,.5,.25),lwd=.05)}; lines(lowess(m4[,1]),col="red",lwd=2.25)
plot(m4[,2],cex=1.25,pch=19,xlim=c(1,177),xlab="Precincts",las=2)
for(i in m4[,2]){abline(h=i,col=rgb(.5,0,0,.25),lwd=.05)}; lines(lowess(m4[,2]),col="blue",lwd=2.25)

jpeg_create()
par(mfrow=c(2,1))
plot(m4[,2],cex=1.25,pch=19,xlim=c(1,177),ylim=c(0,150),xlab="Precincts",las=2)
for(i in m4[,2]){abline(h=i,col=rgb(.5,0,0,.25),lwd=.05)}; lines(lowess(m4[,2]),col="blue",lwd=2.25)
plot(m4[,1],cex=1.25,pch=19,xlim=c(1,177),ylim=c(-150,-0),xlab="Precincts",las=2)
for(i in m4[,1]){abline(h=i,col=rgb(0,0,.5,.25),lwd=.05)}; lines(lowess(m4[,1]),col="red",lwd=2.25)
par(mfrow=c(1,1))

graphics.off()


# under construction
# with persp,persp3D
m1 <- as.matrix(a1_LG1_LG2)
m3 <- m1[,c(8,9)]
persp(x =1:177, y = 1:2, z = m3,
         phi = 20,  theta = -60,
         xlab = "177 Precincts: 101-801", ylab = "votes", zlab = "",
main = "Matrix1: Jail No vs. Jail Yes",
         col = rainbow(177), border = "black", shade = 0.8,
         ticktype = "detailed", d = 2, cex.axis = 1e-9)
 
persp3D(x =1:177, y = 1:2, z = m3,
         bty = "g", phi = 20,  theta = -60,
         xlab = "177 Precincts: 101-801", ylab = "votes", zlab = "",
main = "Matrix1: Jail No vs. Jail Yes",
         zlim=c(min(m3),max(m3)),col = rainbow(177), border = "black", shade = 0.8,
         ticktype = "detailed", space = 0.15, d = 2, cex.axis = 1e-9)

No comments:

Post a Comment