## FIGURE XXX_AL: sb<- read.delim(paste('/mnt/shares/lsa/base/AL/sim105/blindSP.txt',sep=''),header=T) w$CBpolsby[s] <- cor( sb$Polsby[1:999], sb$Polsby[2:1000] ) pdf('/mnt/shares/lsa/base/R/Figure_XXX_AL.pdf',width=8,height=8) plot(sb$Polsby[1:999], sb$Polsby[2:1000],main="Comparison of Compactness Scores of Successive\nComputer-Simulated Plans for Alabama (Race-Blind Algorithm)", xlab="Polsby-Popper Compactness Score of the i-th Simulated Plan for Alabama",ylab="Polsby-Popper Compactness Score of the (i+1)-th Simulated Plan for Alabama") dev.off() col <- function(z){ cols <- rep(999,length(z)) for (i in 1:length(z)){ print(i) touchcols <- cols[seq(1:length(z))[gIntersects(z[i], z,byid=TRUE)]] availcols <- seq(1:999)[!seq(1:999) %in% touchcols]; cols[i] <- min(availcols) } return (cols) } simfolder <- paste('/mnt/shares/lsa/base/',sep=''); sts<-list.files(simfolder); sts<-sts[nchar(sts)==2]; sts<-sts[file.exists(paste(simfolder,sts,'/blind_sp',sep=''))] ## sts <- sts[!(sts %in% c('NJ','PA'))] w <- as.data.frame(sts); names(w)<-'st' w$race20 <- as.numeric(lapply(w$st, function(st) length(list.files(paste(simfolder,st,'/race_sp',sep=''))))) w$blind <- as.numeric(lapply(w$st, function(st) length(list.files(paste(simfolder,st,'/blind_sp',sep=''))))); sts<-sts[as.numeric(lapply(paste(simfolder,sts,'/race_sp',sep=''), function(i) length(list.files(i))))>50] library(maptools); library(rgeos) source(paste("/mnt/shares/lsa/base/jar/R/reock.R",sep="")) source(paste("/mnt/shares/lsa/base/jar/R/pols.R",sep="")); source(paste("/mnt/shares/lsa/base/jar/R/label.R",sep="")) ## 1st three RACE-BLIND maps of AL: library(maptools); library(rgeos) st <- "AL"; simfolder <- paste('/mnt/shares/lsa/base/',sep=''); v <- readShapePoly(paste(simfolder,st,'/base.shp',sep='')) ct <- unionSpatialPolygons(v, v$cty); stpoly<-unionSpatialPolygons(ct, rep(1,length(ct)) ) stfolder <- paste(simfolder,st,'/blind_sp',sep=''); pdf(paste("/mnt/shares/lsa/base/R/Alabama_blind.pdf",sep=""),width=8.5,height=11) for (s in 1:3){ a <- read.delim(paste(stfolder,'/',s,'.txt',sep=''), header=F); a$V1<-as.character(a$V1); out<-as.data.frame(t(a[2:7,2])); names(out)<-a[2:7,1]; v$sim <- as.numeric(a[1,]); shs <- unionSpatialPolygons(v,v$sim); shs <- shs[order(as.numeric(as.character(names(shs))))] cols <- col(shs); cents <- coordinates(shs); for (i in 1:length(shs)){cents[i,] <- labpt(shs[i])} par(mar=c(0,0,0,0)) q <- bbox(ct) plot(ct, xlim=c(q[1,1]+0,q[1,2]-0),ylim=c(q[2,1]-0,q[2,2]-0),border=6,lwd=2,main.cex=3, main=paste("\n\nComputer-Simulated Plan #",s," of 1,000: Race-Blind, Simulated House Plan (105 Districts)")) plot(shs, col=rgb(t(col2rgb( cols )), alpha=50, maxColorValue=255), border="black", cex=0.01,lwd=0.01,add=TRUE) text(cents[,1], cents[,2], labels= names(shs), col="black",cex=0.5,font=1) } dev.off() vv <- read.delim(paste(simfolder,st,'/votes_java2.txt',sep=''),header=T) vv$mcd2 <- paste("00000",vv$mcd,sep=''); vv$mcd2<-substr(vv$mcd2,nchar(vv$mcd2)-4,nchar(vv$mcd2)) for (s in 1:3){ a <- read.delim(paste(stfolder,'/',s,'.txt',sep=''), header=F); a$V1<-as.character(a$V1); out<-as.data.frame(t(a[2:9,2])); names(out)<-a[2:9,1]; vv$sim <- as.numeric(a[1,]); agg <- aggregate(vv[,'Tpop'],by=list(vv$sim),FUN=sum) minpop<-min(agg$x); maxpop<-max(agg$x); totdev <- (max(agg$x)-min(agg$x))/(sum(agg$x)/105) black<-a$V2[6]; polsby <- a$V2[8] vv$code<-paste(substr(vv$IDs,1,3),vv$sim,sep='_'); ag <- aggregate(vv[,'Tpop'],by=list(vv$code),FUN=sum); ag$cty<-substr(ag$Group.1,1,3); ag$dist<-as.numeric(substr(ag$Group.1,5,9)) q<-summary(as.factor(ag$cty),maxsum=9999); q<-q[q>1]; spctys<-length(q); ctyfrags<-dim(ag)[1] vv$code<-paste(substr(vv$IDs,1,3),vv$sim,sep='_'); ag <- aggregate(vv[,'Tpop'],by=list(vv$code),FUN=sum); ag$cty<-substr(ag$Group.1,1,3); ag$dist<-as.numeric(substr(ag$Group.1,5,9)) q<-summary(as.factor(ag$cty),maxsum=9999); q<-q[q>1]; spctys<-length(q); ctyfrags<-dim(ag)[1] vv$code<-paste(vv$mcd2,vv$sim,sep='_'); ag <- aggregate(vv[,'Tpop'],by=list(vv$code),FUN=sum); ag$mcd<-substr(ag$Group.1,1,5); ag$dist<-as.numeric(substr(ag$Group.1,7,99)) ag<-ag[ag$mcd!="99999",]; q<-summary(as.factor(ag$mcd),maxsum=9999); q<-q[q>1]; spmcds<-length(q); mcdfrags<-dim(ag)[1] print( cbind(minpop,maxpop,totdev,black,polsby,a$V3[6],spctys,ctyfrags,spmcds,mcdfrags) ) } ######### END: ######################### MakeHist <- function(repdistricts,xlabel, enactedRS,maintitle){ repdistricts <- repdistricts[!is.na(repdistricts)] z <- summary(as.factor(repdistricts)) xrange <- max(c(repdistricts,enactedRS)) - min(c(repdistricts,enactedRS)) xl <- c( max(-0.5,min(c(repdistricts,enactedRS))-max(1.0,xrange/20)) ,max(c(repdistricts,enactedRS))+max(1.0,xrange/20)) par(mar=c(3.5,4.5,4.8,0.9)) plot(rep(0,length(repdistricts))~repdistricts, xlim=xl, ylim=c(0, max(z)*1.15), type="n",bty="n", ylab="", xaxt="n", yaxt="n",xlab="") title(xlab="", mgp=c(4.25, 1.70, 0)) axis(1,at=seq(from=0,to=10000),labels=paste(seq(from=0,to=10000),sep=""), mgp=c(1.3, 1, 0), cex.axis=1.0) yl <- seq(from=0,to=1000,by=5) axis(2,at=yl,labels=yl, mgp=c(2.5, 1.0, 0), cex.axis=1.0, las=1) t <- 0.35 for (i in 1:length(names(z))){ s <- as.numeric(names(z)[i]) rect(s-t,0,s+t, as.numeric(z[i]) ) percent <- 100*(as.numeric(z[i])/length(repdistricts)) text(s, par("usr")[3]/2, paste(round(percent,2),"%",sep=""), cex=min(0.85, 10/xrange) ) } lines(c(enactedRS,enactedRS),c(0,max(z)*0.99),lty="dashed",col="red") text(enactedRS, max(z)*1.10, labels="Enacted\nPlan", col="red",cex=min(1.5, 15/xrange) ) title(main=maintitle, cex.main=1.3,mgp=c(3.3, 1.70, 0)) title(xlab=xlabel, cex.lab=1.0,mgp=c(2.3, 1.70, 0)) title(ylab=paste("Frequency Among Simulated Districting Plans\n(",length(repdistricts)," Total Simulated Plans)",sep=""), mgp=c(2.4, 1.0, 0),cex.lab=1.0) } ## library(maptools); library(rgdal); library(rgeos); library(foreign) library("foreach"); library("doParallel") ##library(ggplot2) ################### simfolder <- paste('/mnt/shares/lsa/base/',sep=''); st <- c('AL','AR','AZ','CA','DE','FL','GA','IL','LA','MD','MS','NC','NM','NV','NY','SC','TN','TX','VA') w <- as.data.frame(st); w$sfx <- '_sp'; w$blind <- as.numeric(lapply(1:dim(w)[1], function(i) length(list.files(paste(simfolder,w$st[i],'/blind',w$sfx[i],sep=''))))); w$race <- as.numeric(lapply(1:dim(w)[1], function(i) length(list.files(paste(simfolder,w$st[i],'/race',w$sfx[i],sep=''))))); for (j in c(1:19)){ st<-w$st[j]; print(st) files<-list.files(paste(simfolder,st,sep='')); files<-files[regexpr('sim',files)==1][1]; sldl_dists <- as.numeric(substr(files,4,99)); v <- read.delim(paste(simfolder,st,'/votes_java2.txt',sep=''),header=T) ## RACE-CONSCIOUS SIMULATIONS: stfolder <- paste(simfolder,st,'/race',w$sfx[j],sep=''); cl <- makeCluster(1000); registerDoParallel(cl) res = foreach(i = 1:1000, .combine = "rbind", .packages = c()) %dopar% { a <- read.delim(paste(stfolder,'/',i,'.txt',sep=''), header=F); a$V1<-as.character(a$V1); out<-as.data.frame(t(a[2:7,2])); names(out)<-a[2:7,1]; v$sim <- as.numeric(a[1,]); ag <- aggregate(v[,c(12:37)],by=list(v$sim),FUN=function(k) sum(k,na.rm=T)); ag$dist<-substr(ag$Group.1,1,3); ##q<-summary(as.factor(v$cty)); q<-q[q>1]; d$spctys<-length(q); d$ctyfrags<-dim(a)[1] ##58 split ctys ##ag$ctyfrags <- as.numeric(lapply(ag$dist, function(i) length(unique(v$cty[v$sim==i])) )) ##ag$mcdfrags <- as.numeric(lapply(ag$dist, function(i) length(unique(v$mcd[v$sim==i & v$mcd!="99999"])) )) ag$Brsh <- ag$BRvotes/(ag$BRvotes+ag$BDvotes); ag$Hrsh <- ag$HRvotes/(ag$HRvotes+ag$HDvotes); ag$bshareDEMS <- ag$BDvotes/(ag$BDvotes+ag$HDvotes+ag$ODvotes); ag$bshareREPS <- ag$BRvotes/(ag$BRvotes+ag$HRvotes+ag$ORvotes); ## black share among all Obama voters; ag$hshareDEMS <- ag$HDvotes/(ag$BDvotes+ag$HDvotes+ag$ODvotes); ag$hshareREPS <- ag$HRvotes/(ag$BRvotes+ag$HRvotes+ag$ORvotes); ## hispanic share among all Obama voters; ag$bhshareDEMS <- (ag$BDvotes+ag$HDvotes)/ag$Obama; ag$bhshareREPS <- (ag$BRvotes+ag$HRvotes)/ag$Romney; ## black+hispanic share among all Obama voters; ag$type<-""; ag$rsh<-(ag$BRvotes+ag$HRvotes+ag$ORvotes)/(ag$BRvotes+ag$HRvotes+ag$ORvotes+ag$BDvotes+ag$HDvotes+ag$ODvotes) ag$type[ag$bshareDEMS+ag$hshareDEMS>0.5 & ag$Brsh<0.5 & ag$Hrsh<0.5 & ag$rsh<0.5 & ag$bshareDEMS>ag$hshareDEMS] <- "BlackCoalition"; ag$type[ag$bshareDEMS+ag$hshareDEMS>0.5 & ag$Brsh>0.5 & ag$Hrsh>0.5 & ag$rsh>0.5 & ag$bshareDEMS>ag$hshareDEMS] <- "BlackCoalition"; ag$type[ag$bshareDEMS+ag$hshareDEMS>0.5 & ag$Brsh<0.5 & ag$Hrsh<0.5 & ag$rsh<0.5 & ag$bshareDEMS0.5 & ag$Brsh>0.5 & ag$Hrsh>0.5 & ag$rsh>0.5 & ag$bshareDEMS0.5 & ag$Brsh<0.5 & ag$rsh<0.5] <- "Black"; ag$type[ag$bshareREPS>0.5 & ag$Brsh>0.5 & ag$rsh>0.5] <- "Black"; ag$type[ag$hshareDEMS>0.5 & ag$Hrsh<0.5 & ag$rsh<0.5] <- "Hisp"; ag$type[ag$hshareREPS>0.5 & ag$Hrsh>0.5 & ag$rsh>0.5] <- "Hisp"; out$BlackDists <- sum(ag$type %in% c("Black","BlackCoalition")); out$HispDists <- sum(ag$type %in% c("Hisp","HispCoalition")) out$romney.sw<- sum(ag$BRvotes+ag$HRvotes+ag$ORvotes)/sum(ag$BRvotes+ag$HRvotes+ag$ORvotes+ag$BDvotes+ag$HDvotes+ag$ODvotes) out$romneydists<-sum(ag$rsh>0.5); out$romney.med<-median(ag$rsh); out$romney.mean<-mean(ag$rsh); return(out) ## table(ag$type); a[2:7,1:5] ##agg <- aggregate(v[,c(12:37)],by=list(v$sim),FUN=function(k) sum(k,na.rm=T)); agg$romshare<-(agg$BRvotes+agg$HRvotes+agg$ORvotes)/(agg$BRvotes+agg$HRvotes+agg$ORvotes+agg$BDvotes+agg$HDvotes+agg$ODvotes) ##agg$romshare2<-agg$Romney/(agg$Romney+agg$Obama); } write.table(res, paste(simfolder,st,'/sim',sldl_dists,'/raceSP.txt',sep=''), row.names=F,col.names=T,sep='\t'); print(paste(simfolder,st,'/sim',sldl_dists,'/race4.txt',sep='')) cl <- makeCluster(1000); registerDoParallel(cl) res = foreach(i = 1:1000, .combine = "rbind", .packages = c()) %dopar% { a <- read.delim(paste(stfolder,'/',i,'.txt',sep=''), header=F); a$V1<-as.character(a$V1); v$sim <- as.numeric(a[1,]); agg <- aggregate(v[,c(12:37)],by=list(v$sim),FUN=function(k) sum(k,na.rm=T)); agg$romshare<-(agg$BRvotes+agg$HRvotes+agg$ORvotes)/(agg$BRvotes+agg$HRvotes+agg$ORvotes+agg$BDvotes+agg$HDvotes+agg$ODvotes) ##agg$romshare2<-agg$Romney/(agg$Romney+agg$Obama); agg$BshDems<-agg$BDvotes/agg$Obama; agg$BshReps<-agg$BRvotes/agg$Romney; agg$BshMajPty<-agg$BshDems; agg$BshMajPty[agg$Romney>agg$Obama]<-agg$BshReps[agg$Romney>agg$Obama]; sum(agg$BshMajPty > 0.5) if(sum(is.na(agg$romshare))==0){return(as.data.frame(t(sort(agg$romshare))));} } poloutfile <- paste(simfolder,st,'/sim',sldl_dists,'/raceSP.romshare.txt',sep=''); write.table(res, poloutfile,row.names=F,col.names=F,sep='\t'); cl <- makeCluster(1000); registerDoParallel(cl) res = foreach(i = 1:1000, .combine = "rbind", .packages = c()) %dopar% { a <- read.delim(paste(stfolder,'/',i,'.txt',sep=''), header=F); a$V1<-as.character(a$V1); v$sim <- as.numeric(a[1,]); agg <- aggregate(v[,c(12:37)],by=list(v$sim),FUN=function(k) sum(k,na.rm=T)); agg$right<-agg$BRvotes+agg$HRvotes+agg$ORvotes; agg$left<-agg$BDvotes+agg$HDvotes+agg$ODvotes; agg$romshare<-agg$right/(agg$right+agg$left); ##agg$romshare2<-agg$Romney/(agg$Romney+agg$Obama); agg$BshDems<-agg$BDvotes/agg$left; agg$BshReps<-agg$BRvotes/agg$right; agg$BshMajPty<-agg$BshDems; agg$BshMajPty[agg$right>agg$left]<-agg$BshReps[agg$right>agg$left]; ##sum(agg$BshMajPty > 0.5) if(sum(is.na(agg$BshMajPty))==0){return(as.data.frame(t(sort(agg$BshMajPty)))); } } bckoutfile <- paste(simfolder,st,'/sim',sldl_dists,'/raceSP.BshMajPty.txt',sep=''); write.table(res, bckoutfile,row.names=F,col.names=F,sep='\t'); cl <- makeCluster(1000); registerDoParallel(cl) res = foreach(i = 1:1000, .combine = "rbind", .packages = c()) %dopar% { a <- read.delim(paste(stfolder,'/',i,'.txt',sep=''), header=F); a$V1<-as.character(a$V1); v$sim <- as.numeric(a[1,]); agg <- aggregate(v[,c(12:37)],by=list(v$sim),FUN=function(k) sum(k,na.rm=T)); agg$right<-agg$BRvotes+agg$HRvotes+agg$ORvotes; agg$left<-agg$BDvotes+agg$HDvotes+agg$ODvotes; agg$romshare<-agg$right/(agg$right+agg$left); ##agg$romshare2<-agg$Romney/(agg$Romney+agg$Obama); agg$HshDems<-agg$HDvotes/agg$left; agg$HshReps<-agg$HRvotes/agg$right; agg$HshMajPty<-agg$HshDems; agg$HshMajPty[agg$right>agg$left]<-agg$HshReps[agg$right>agg$left]; if(sum(is.na(agg$HshMajPty))==0){return(as.data.frame(t(sort(agg$HshMajPty)))); } } hckoutfile <- paste(simfolder,st,'/sim',sldl_dists,'/raceSP.HshMajPty.txt',sep=''); write.table(res, hckoutfile,row.names=F,col.names=F,sep='\t'); ## RACE-BLIND SIMULATIONS: stfolder <- paste(simfolder,st,'/blind',w$sfx[j],sep=''); cl <- makeCluster(1000); registerDoParallel(cl) cl <- makeCluster(1000); registerDoParallel(cl) res = foreach(i = 1:1000, .combine = "rbind", .packages = c()) %dopar% { a <- read.delim(paste(stfolder,'/',i,'.txt',sep=''), header=F); a$V1<-as.character(a$V1); out<-as.data.frame(t(a[2:7,2])); names(out)<-a[2:7,1]; v$sim <- as.numeric(a[1,]); ag <- aggregate(v[,c(12:37)],by=list(v$sim),FUN=function(k) sum(k,na.rm=T)); ag$dist<-substr(ag$Group.1,1,3); ##q<-summary(as.factor(v$cty)); q<-q[q>1]; d$spctys<-length(q); d$ctyfrags<-dim(a)[1] ##58 split ctys ##ag$ctyfrags <- as.numeric(lapply(ag$dist, function(i) length(unique(v$cty[v$sim==i])) )) ##ag$mcdfrags <- as.numeric(lapply(ag$dist, function(i) length(unique(v$mcd[v$sim==i & v$mcd!="99999"])) )) ag$Brsh <- ag$BRvotes/(ag$BRvotes+ag$BDvotes); ag$Hrsh <- ag$HRvotes/(ag$HRvotes+ag$HDvotes); ag$bshareDEMS <- ag$BDvotes/(ag$BDvotes+ag$HDvotes+ag$ODvotes); ag$bshareREPS <- ag$BRvotes/(ag$BRvotes+ag$HRvotes+ag$ORvotes); ## black share among all Obama voters; ag$hshareDEMS <- ag$HDvotes/(ag$BDvotes+ag$HDvotes+ag$ODvotes); ag$hshareREPS <- ag$HRvotes/(ag$BRvotes+ag$HRvotes+ag$ORvotes); ## hispanic share among all Obama voters; ag$bhshareDEMS <- (ag$BDvotes+ag$HDvotes)/ag$Obama; ag$bhshareREPS <- (ag$BRvotes+ag$HRvotes)/ag$Romney; ## black+hispanic share among all Obama voters; ag$type<-""; ag$rsh<-(ag$BRvotes+ag$HRvotes+ag$ORvotes)/(ag$BRvotes+ag$HRvotes+ag$ORvotes+ag$BDvotes+ag$HDvotes+ag$ODvotes) ag$type[ag$bshareDEMS+ag$hshareDEMS>0.5 & ag$Brsh<0.5 & ag$Hrsh<0.5 & ag$rsh<0.5 & ag$bshareDEMS>ag$hshareDEMS] <- "BlackCoalition"; ag$type[ag$bshareDEMS+ag$hshareDEMS>0.5 & ag$Brsh>0.5 & ag$Hrsh>0.5 & ag$rsh>0.5 & ag$bshareDEMS>ag$hshareDEMS] <- "BlackCoalition"; ag$type[ag$bshareDEMS+ag$hshareDEMS>0.5 & ag$Brsh<0.5 & ag$Hrsh<0.5 & ag$rsh<0.5 & ag$bshareDEMS0.5 & ag$Brsh>0.5 & ag$Hrsh>0.5 & ag$rsh>0.5 & ag$bshareDEMS0.5 & ag$Brsh<0.5 & ag$rsh<0.5] <- "Black"; ag$type[ag$bshareREPS>0.5 & ag$Brsh>0.5 & ag$rsh>0.5] <- "Black"; ag$type[ag$hshareDEMS>0.5 & ag$Hrsh<0.5 & ag$rsh<0.5] <- "Hisp"; ag$type[ag$hshareREPS>0.5 & ag$Hrsh>0.5 & ag$rsh>0.5] <- "Hisp"; out$BlackDists <- sum(ag$type %in% c("Black","BlackCoalition")); out$HispDists <- sum(ag$type %in% c("Hisp","HispCoalition")) out$romney.sw<- sum(ag$BRvotes+ag$HRvotes+ag$ORvotes)/sum(ag$BRvotes+ag$HRvotes+ag$ORvotes+ag$BDvotes+ag$HDvotes+ag$ODvotes) out$romneydists<-sum(ag$rsh>0.5); out$romney.med<-median(ag$rsh); out$romney.mean<-mean(ag$rsh); return(out) ## table(ag$type); a[2:7,1:5] ##agg <- aggregate(v[,c(12:37)],by=list(v$sim),FUN=function(k) sum(k,na.rm=T)); agg$romshare<-(agg$BRvotes+agg$HRvotes+agg$ORvotes)/(agg$BRvotes+agg$HRvotes+agg$ORvotes+agg$BDvotes+agg$HDvotes+agg$ODvotes) ##agg$romshare2<-agg$Romney/(agg$Romney+agg$Obama); } write.table(res, paste(simfolder,st,'/sim',sldl_dists,'/blindSP.txt',sep=''), row.names=F,col.names=T,sep='\t'); print(paste(simfolder,st,'/sim',sldl_dists,'/blind.txt',sep='')) cl <- makeCluster(1000); registerDoParallel(cl) res = foreach(i = 1:1000, .combine = "rbind", .packages = c()) %dopar% { a <- read.delim(paste(stfolder,'/',i,'.txt',sep=''), header=F); a$V1<-as.character(a$V1); v$sim <- as.numeric(a[1,]); agg <- aggregate(v[,c(12:37)],by=list(v$sim),FUN=function(k) sum(k,na.rm=T)); agg$romshare<-(agg$BRvotes+agg$HRvotes+agg$ORvotes)/(agg$BRvotes+agg$HRvotes+agg$ORvotes+agg$BDvotes+agg$HDvotes+agg$ODvotes) ##agg$romshare2<-agg$Romney/(agg$Romney+agg$Obama); agg$BshDems<-agg$BDvotes/agg$Obama; agg$BshReps<-agg$BRvotes/agg$Romney; agg$BshMajPty<-agg$BshDems; agg$BshMajPty[agg$Romney>agg$Obama]<-agg$BshReps[agg$Romney>agg$Obama]; sum(agg$BshMajPty > 0.5) if(sum(is.na(agg$romshare))==0){return(as.data.frame(t(sort(agg$romshare)))); } } poloutfile <- paste(simfolder,st,'/sim',sldl_dists,'/blindSP.romshare.txt',sep=''); write.table(res, poloutfile,row.names=F,col.names=F,sep='\t'); cl <- makeCluster(1000); registerDoParallel(cl) res = foreach(i = 1:1000, .combine = "rbind", .packages = c()) %dopar% { a <- read.delim(paste(stfolder,'/',i,'.txt',sep=''), header=F); a$V1<-as.character(a$V1); v$sim <- as.numeric(a[1,]); agg <- aggregate(v[,c(12:37)],by=list(v$sim),FUN=function(k) sum(k,na.rm=T)); agg$right<-agg$BRvotes+agg$HRvotes+agg$ORvotes; agg$left<-agg$BDvotes+agg$HDvotes+agg$ODvotes; agg$romshare<-agg$right/(agg$right+agg$left); ##agg$romshare2<-agg$Romney/(agg$Romney+agg$Obama); agg$BshDems<-agg$BDvotes/agg$left; agg$BshReps<-agg$BRvotes/agg$right; agg$BshMajPty<-agg$BshDems; agg$BshMajPty[agg$right>agg$left]<-agg$BshReps[agg$right>agg$left]; ##sum(agg$BshMajPty > 0.5) if(sum(is.na(agg$BshMajPty))==0){return(as.data.frame(t(sort(agg$BshMajPty)))); } } bckoutfile <- paste(simfolder,st,'/sim',sldl_dists,'/blindSP.BshMajPty.txt',sep=''); write.table(res, bckoutfile,row.names=F,col.names=F,sep='\t'); cl <- makeCluster(1000); registerDoParallel(cl) res = foreach(i = 1:1000, .combine = "rbind", .packages = c()) %dopar% { a <- read.delim(paste(stfolder,'/',i,'.txt',sep=''), header=F); a$V1<-as.character(a$V1); v$sim <- as.numeric(a[1,]); agg <- aggregate(v[,c(12:37)],by=list(v$sim),FUN=function(k) sum(k,na.rm=T)); agg$right<-agg$BRvotes+agg$HRvotes+agg$ORvotes; agg$left<-agg$BDvotes+agg$HDvotes+agg$ODvotes; agg$romshare<-agg$right/(agg$right+agg$left); ##agg$romshare2<-agg$Romney/(agg$Romney+agg$Obama); agg$HshDems<-agg$HDvotes/agg$left; agg$HshReps<-agg$HRvotes/agg$right; agg$HshMajPty<-agg$HshDems; agg$HshMajPty[agg$right>agg$left]<-agg$HshReps[agg$right>agg$left]; if(sum(is.na(agg$HshMajPty))==0){return(as.data.frame(t(sort(agg$HshMajPty)))); } } hckoutfile <- paste(simfolder,st,'/sim',sldl_dists,'/blindSP.HshMajPty.txt',sep=''); write.table(res, hckoutfile,row.names=F,col.names=F,sep='\t'); } ## FIGURE 2: COMPACTNESS, ALL STATES IN A SINGLE FIGURE simfolder <- paste('/mnt/shares/lsa/base/',sep=''); st <- c('AL','AR','AZ','CA','DE','FL','GA','IL','LA','MD','MS','NC','NM','NV','NY','SC','TN','TX','VA') w <- as.data.frame(st); w$sfx <- 20; w$sfx[w$st %in% c('AZ','DE','LA','MD','NC','NY','TX')]<-'_may' w$race20 <- as.numeric(lapply(w$st, function(st) length(list.files(paste(simfolder,st,'/race_sp',sep=''))))) w$blind <- as.numeric(lapply(w$st, function(st) length(list.files(paste(simfolder,st,'/blind_sp',sep=''))))); w <- w[rev(order(w$st)),] pdf('/mnt/shares/lsa/base/R/Figure2_Simulations_Compactness.pdf',width=8.5,height=11) par(mar=c(4,5,3,3)); plot(c(NA)~c(NA),xlim=c(0.15,0.45),ylim=c(1,dim(w)[1]+1),xaxt="n",yaxt="n",xlab="Polsby-Popper Score of Legislative Districting Plan\n",ylab="") title(main="Compactness of Race-Blind Simulations",cex=1.7) xl <- seq(0,1,by=0.05); axis(1, at=xl, cex.axis=1.3, labels=xl, mgp=c(1.3, 0.9, 0) ,las=1) axis(2, at=1:dim(w)[1], labels=w$st,las=1) for (s in c(1:19)){ st<-w$st[s]; lines(c(0,1),c(s,s),lty="dashed",col="gray") files<-list.files(paste(simfolder,st,sep='')); files<-files[regexpr('sim',files)==1]; sldl_dists <- as.numeric(substr(files,4,99)); e <- read.delim(paste(simfolder,st,'/sim',sldl_dists,'/enact.txt',sep=''),header=T) sb<- read.delim(paste(simfolder,st,'/sim',sldl_dists,'/blindSP.txt',sep=''),header=T) points(sb$Polsby, runif(dim(sb)[1],s-0.25,s+0.25) , pch=1,col="gray" ); points(e$polsby, s, pch=8,col="red" ) } legend("top", c(paste("1,000 Race-Blind Computer-Simulated House Districting Plans",sep=''),"Enacted House Plans (as of 2016)") , pch=c(1,8), cex=c(1,1), pt.cex=c(1,1), col=c("gray","red"), text.col=c("black","red"), title.col=c("black") ,bty="y" ) par(mar=c(4,5,3,3)); plot(c(NA)~c(NA),xlim=c(0.15,0.45),ylim=c(1,dim(w)[1]+1),xaxt="n",yaxt="n",xlab="Polsby-Popper Score of Legislative Districting Plan\n",ylab="" ) title(main="Compactness of Race-Conscious Simulations",cex=1.7); xl <- seq(0,1,by=0.05); axis(1, at=xl, cex.axis=1.3, labels=xl, mgp=c(1.3, 0.9, 0) ,las=1) axis(2, at=1:dim(w)[1], labels=w$st,las=1) for (s in 1:dim(w)[1]){ st<-w$st[s]; lines(c(0,1),c(s,s),lty="dashed",col="gray") files<-list.files(paste(simfolder,st,sep='')); files<-files[regexpr('sim',files)==1]; sldl_dists <- as.numeric(substr(files,4,99)); e <- read.delim(paste(simfolder,st,'/sim',sldl_dists,'/enact.txt',sep=''),header=T) sr<- read.delim(paste(simfolder,st,'/sim',sldl_dists,'/raceSP.txt',sep=''),header=T) points(sr$Polsby, runif(dim(sr)[1],s-0.25,s+0.25) , pch=1,col="gray" ); points(e$polsby, s, pch=8,col="red" ) } legend("top", c(paste("1,000 Race-Conscious Computer-Simulated House Districting Plans",sep=''),"Enacted House Plans (as of 2016)") , pch=c(1,8), cex=c(1,1), pt.cex=c(1,1), col=c("gray","red"), text.col=c("black","red"), title.col=c("black") ,bty="y" ) dev.off() ## ################## NOW MAKE DISTRICT-LEVEL FIGURES FOR EACH CHAMBER SIZE, SHOWING THE EXACT HISPANIC PROPORTION fp <- read.delim('/mnt/shares/lsa/CensusBlockData/baf/fips.txt',header=T); fp$Numeric.code<-paste('0',fp$Numeric.code,sep=''); fp$Numeric.code<-substr(fp$Numeric.code,nchar(fp$Numeric.code)-1,nchar(fp$Numeric.code)) ######################### MakeHist <- function(repdistricts,xlabel, enactedRS,maintitle){ repdistricts <- repdistricts[!is.na(repdistricts)] z <- summary(as.factor(repdistricts)) xrange <- max(c(repdistricts,enactedRS)) - min(c(repdistricts,enactedRS)) xl <- c( max(-0.5,min(c(repdistricts,enactedRS))-max(1.0,xrange/20)) ,max(c(repdistricts,enactedRS))+max(1.0,xrange/20)) par(mar=c(3.5,4.5,4.8,0.9)) plot(rep(0,length(repdistricts))~rep(0,length(repdistricts)), xlim=xl, ylim=c(0, max(z)*1.15), type="n",bty="n", ylab="", xaxt="n", yaxt="n",xlab="") title(xlab="", mgp=c(4.25, 1.70, 0)) axis(1,at=seq(from=0,to=200),labels=paste(seq(from=0,to=200),sep=""), mgp=c(1.3, 1, 0), cex.axis=1.0) yl <- seq(from=0,to=1000,by=50) axis(2,at=yl,labels=yl, mgp=c(2.5, 1.0, 0), cex.axis=1.0, las=1) t <- 0.35 for (i in 1:length(names(z))){ s <- as.numeric(names(z)[i]) rect(s-t,0,s+t, as.numeric(z[i]) ) percent <- 100*(as.numeric(z[i])/length(repdistricts)) text(s, par("usr")[3]/2, paste(round(percent,2),"%",sep=""), cex=min(0.85, 10/xrange) ) } lines(c(enactedRS,enactedRS),c(0,max(z)*0.99),lty="dashed",col="red") text(enactedRS, max(z)*1.10, labels="Enacted\nPlan", col="red",cex=min(1.5, 15/xrange) ) title(main=maintitle, cex.main=1.3,mgp=c(3.3, 1.70, 0)) title(xlab=xlabel, cex.lab=1.0,mgp=c(2.3, 1.70, 0)) title(ylab=paste("Frequency Among Simulated Districting Plans\n(",length(repdistricts)," Total Simulated Plans)",sep=""), mgp=c(2.4, 1.0, 0),cex.lab=1.0) } pdf('/mnt/shares/lsa/base/R/Simulations_OpportunityDistricts.pdf',width=8.5,height=6.5) for (s in 1:dim(w)[1]){ st<-w$st[s] files<-list.files(paste(simfolder,st,sep='')); files<-files[regexpr('sim',files)==1][1]; sldl_dists <- as.numeric(substr(files,4,99)); e <- read.delim(paste(simfolder,st,'/sim',sldl_dists,'/enact.txt',sep=''),header=T) sr<- read.delim(paste(simfolder,st,'/sim',sldl_dists,'/raceSP.txt',sep=''),header=T); sr<-sr[!sr$BlackDists=="Error in file(file, \\rt\\) : cannot open the connection\n",] sb<- read.delim(paste(simfolder,st,'/sim',sldl_dists,'/blindSP.txt',sep=''),header=T); sb<-sb[!sb$BlackDists=="Error in file(file, \\rt\\) : cannot open the connection\n",] ##layout(matrix(c(1,2), 1,2,byrow=TRUE), widths=c(1,1), heights=c(1) ) MakeHist(sb$Black, paste("Number of African-American Opportunity Districts (",sldl_dists," total districts)",sep=''), e$BlackDists,paste(st,": Race-Blind Simulations",sep='')) MakeHist(sr$Black, paste("Number of African-American Opportunity Districts (",sldl_dists," total districts)",sep=''), e$BlackDists,paste(st,": Race-Conscious Simulations",sep='')) MakeHist(sb$Hisp, paste("Number of Hispanic Opportunity Districts (",sldl_dists," total districts)",sep=''), e$HispDists,paste(st,": Race-Blind Simulations",sep='')) MakeHist(sr$Hisp, paste("Number of Hispanic Opportunity Districts (",sldl_dists," total districts)",sep=''), e$HispDists,paste(st,": Race-Conscious Simulations",sep='')) } dev.off() simfolder <- paste('/mnt/shares/lsa/base/',sep=''); st <- c('AL','AR','AZ','CA','DE','FL','GA','IL','LA','MD','MS','NC','NM','NV','NY','SC','TN','TX','VA') w <- as.data.frame(st); w$sfx <- 20; w$sfx[w$st %in% c('AZ','DE','LA','MD','NC','NY','TX')]<-'_may' w$race20 <- as.numeric(lapply(w$st, function(st) length(list.files(paste(simfolder,st,'/race_sp',sep=''))))) w$blind <- as.numeric(lapply(w$st, function(st) length(list.files(paste(simfolder,st,'/blind_sp',sep=''))))); q <- read.delim("/mnt/shares/lsa/base/2018CVAP.txt",header=T) w$eBSeatShare <- as.numeric(lapply(1:dim(w)[1], function(s){ st<-w$st[s] files<-list.files(paste(simfolder,st,sep='')); files<-files[regexpr('sim',files)==1][1]; sldl_dists <- as.numeric(substr(files,4,99)); e <- read.delim(paste(simfolder,st,'/sim',sldl_dists,'/enact.txt',sep=''),header=T) return(e$BlackDists/sldl_dists) })) w<-w[order(w$st),]; w<-w[order(w$eBSeatShare),]; w$bcvap<-q$BlackCVAP[match(w$st,q$st)]; w$hcvap<-q$HispanicCVAP[match(w$st,q$st)]; w1<-w; pdf('/mnt/shares/lsa/base/R/Figure_12_13_SingleFigure_OpportunityDistricts.pdf',width=11,height=8.5) w<-w1; w<-w[w$bcvap>0.09,]; w<-w[rev(order(w$bcvap)),]; w<-w[rev(order(w$st)),] par(mar=c(4.5,3,3,1)); plot(c(NA)~c(NA),xlim=c(0,0.38),ylim=c(1,dim(w)[1]+2.5),xaxt="n",yaxt="n",xlab="African-American Opportunity Districts as Share of All House Districts\n",ylab="",main="African-American Opportunity Districts in Simulated and Enacted House Plans" ) xl <- seq(0,1,by=0.05); axis(1, at=xl, cex.axis=1.1, labels=paste(100*xl,'%',sep=''), mgp=c(1.3, 0.8, 0) ,las=1) axis(2, at=1:dim(w)[1], labels=w$st,las=1) ## axis(4, at=1:dim(w)[1], labels=paste(100*round(w$bcvap,2),'%',sep=''),las=1) for (s in 1:dim(w)[1]){ lines(c(-1,1),c(s,s),lty="dashed",col="black"); st<-w$st[s] files<-list.files(paste(simfolder,st,sep='')); files<-files[regexpr('sim',files)==1][1]; sldl_dists <- as.numeric(substr(files,4,99)); e <- read.delim(paste(simfolder,st,'/sim',sldl_dists,'/enact.txt',sep=''),header=T) ##sr<- read.delim(paste(simfolder,st,'/sim',sldl_dists,'/raceSP.txt',sep=''),header=T) sb<- read.delim(paste(simfolder,st,'/sim',sldl_dists,'/blindSP.txt',sep=''),header=T) points(sb$Black/sldl_dists, runif(dim(sb)[1],s-0.3,s+0.3) , pch=1,col="gray" ); points(e$BlackDists/sldl_dists, s, pch=8,col="red",cex=1.6 ) points(w$bcvap[s], s, pch=1,col="blue",cex=1.3 ) } legend("top", c("African-American Opportunity Districts as Share of Race-Blind Computer-Simulated House Plans","African-American Opportunity Districts as Share of Enacted House Plan","African-American Share of State's Citizen Voting Age Population (CVAP)") , pch=c(1,8,1), cex=c(1.2,1.2,1.2), pt.cex=c(1,1,1), col=c("gray","red","blue"), text.col=c("black","red","blue"), title.col=c("black") ,bty="y" ) ## mtext("African-American Share of State's Citizen Voting Age Population (CVAP)", line = 3,side=4) w<-w1; w<-w[w$hcvap>0.09,]; w<-w[rev(order(w$hcvap)),]; w<-w[rev(order(w$st)),] par(mar=c(4.5,3,3,1)); plot(c(NA)~c(NA),xlim=c(0,0.41),ylim=c(0.8,dim(w)[1]+1.4),xaxt="n",yaxt="n",xlab="Hispanic Opportunity Districts as Share of All House Districts\n",ylab="",main="Hispanic Opportunity Districts in Simulated and Enacted House Plans" ) xl <- seq(0,1,by=0.05); axis(1, at=xl, cex.axis=1.1, labels=paste(100*xl,'%',sep=''), mgp=c(1.3, 0.8, 0) ,las=1) axis(2, at=1:dim(w)[1], labels=w$st,las=1) ##axis(4, at=1:dim(w)[1], labels=paste(100*round(w$hcvap,2),'%',sep=''),las=1) for (s in 1:dim(w)[1]){ lines(c(-1,1),c(s,s),lty="dashed",col="black"); st<-w$st[s] files<-list.files(paste(simfolder,st,sep='')); files<-files[regexpr('sim',files)==1][1]; sldl_dists <- as.numeric(substr(files,4,99)); e <- read.delim(paste(simfolder,st,'/sim',sldl_dists,'/enact.txt',sep=''),header=T) ##sr<- read.delim(paste(simfolder,st,'/sim',sldl_dists,'/raceSP.txt',sep=''),header=T) sb<- read.delim(paste(simfolder,st,'/sim',sldl_dists,'/blindSP.txt',sep=''),header=T) points(sb$Hisp/sldl_dists, runif(dim(sb)[1],s-0.3,s+0.3) , pch=1,col="gray" ); points(e$HispDists/sldl_dists, s, pch=8,col="red",cex=1.6 ) points(w$hcvap[s], s, pch=1,col="blue",cex=1.3 ) } legend("top", c("Hispanic Opportunity Districts as Share of Race-Blind Computer-Simulated House Plans","Hispanic Opportunity Districts as Share of Enacted House Plan","Hispanic Share of State's Citizen Voting Age Population (CVAP)") , pch=c(1,8,1), cex=c(1.2,1.2,1.2), pt.cex=c(1,1,1), col=c("gray","red","blue"), text.col=c("black","red","blue"), title.col=c("black") ,bty="y" ) dev.off() simfolder <- paste('/mnt/shares/lsa/base/',sep=''); st <- c('AL','AR','AZ','CA','DE','FL','GA','IL','LA','MD','MS','NC','NM','NV','NY','SC','TN','TX','VA') w <- as.data.frame(st); w$sfx <- '_sp'; ##w$sfx[w$st %in% c('CA')]<-'_sp5' w$blind <- as.numeric(lapply(1:dim(w)[1], function(i) length(list.files(paste(simfolder,w$st[i],'/blind',w$sfx[i],sep=''))))); w$race <- as.numeric(lapply(1:dim(w)[1], function(i) length(list.files(paste(simfolder,w$st[i],'/race',w$sfx[i],sep=''))))); w <- w[rev(order(w$st)),] pdf('/mnt/shares/lsa/base/R/Figure_15_16_SingleFigure_RepublicanDistricts.pdf',width=8.5,height=11) par(mar=c(4.5,3,3,1)); plot(c(NA)~c(NA),xlim=c(0,1),ylim=c(1,dim(w)[1]+2.4),xaxt="n",yaxt="n",xlab="Republican Share of All House Districts\n",ylab="",main="Republican Share of All House Districts\nin Enacted House Plans and Race-Conscious Simulated Plans" ) xl <- seq(0,1,by=0.05); axis(1, at=xl, cex.axis=1.1, labels=paste(100*xl,'%',sep=''), mgp=c(1.3, 0.8, 0) ,las=1) axis(2, at=1:dim(w)[1], labels=w$st,las=1) for (s in 1:dim(w)[1]){ lines(c(-1,1),c(s+0.5,s+0.5),lty="dashed",col="black"); st<-w$st[s] files<-list.files(paste(simfolder,st,sep='')); files<-files[regexpr('sim',files)==1][1]; sldl_dists <- as.numeric(substr(files,4,99)); e <- read.delim(paste(simfolder,st,'/sim',sldl_dists,'/enact_dists.txt',sep=''),header=T) sr<- read.delim(paste(simfolder,st,'/sim',sldl_dists,'/raceSP.txt',sep=''),header=T); sr<-sr[!sr$BlackDists=="Error in file(file, \\rt\\) : cannot open the connection\n",]; sr$romneydists<-as.numeric(as.character(sr$romneydists)) sb<- read.delim(paste(simfolder,st,'/sim',sldl_dists,'/blindSP.txt',sep=''),header=T); sb<-sb[!sb$BlackDists=="Error in file(file, \\rt\\) : cannot open the connection\n",] ##points(sb$romneydists/sldl_dists, runif(dim(sb)[1],s+0.09,s+0.38) , pch=1,col="gray" ); points(sr$romneydists/sldl_dists, runif(dim(sr)[1],s-0.25,s+0.25) , pch=1,col="blue" ); points(sum(e$rsh>0.5)/sldl_dists, s, pch=8,col="red",cex=1.9 ) } legend("top", c("Race-Conscious Computer-Simulated House Districting Plans","Enacted House Plans (as of 2016)") , pch=c(1,8), cex=c(1.2,1.2), pt.cex=c(1,1), col=c("blue","red"), text.col=c("blue","red"), title.col=c("black") ,bty="y" ) par(mar=c(4.5,3,3,1)); plot(c(NA)~c(NA),xlim=c(0,1),ylim=c(1,dim(w)[1]+2.4),xaxt="n",yaxt="n",xlab="Republican Share of All House Districts\n",ylab="",main="Republican Share of All House Districts\nin Race-Conscious and Race-Blind Simulated House Plans" ) xl <- seq(0,1,by=0.05); axis(1, at=xl, cex.axis=1.1, labels=paste(100*xl,'%',sep=''), mgp=c(1.3, 0.8, 0) ,las=1) axis(2, at=1:dim(w)[1], labels=w$st,las=1) for (s in 1:dim(w)[1]){ lines(c(-1,1),c(s+0.5,s+0.5),lty="dashed",col="black"); st<-w$st[s] files<-list.files(paste(simfolder,st,sep='')); files<-files[regexpr('sim',files)==1][1]; sldl_dists <- as.numeric(substr(files,4,99)); e <- read.delim(paste(simfolder,st,'/sim',sldl_dists,'/enact_dists.txt',sep=''),header=T) sr<- read.delim(paste(simfolder,st,'/sim',sldl_dists,'/raceSP.txt',sep=''),header=T); sr<-sr[!sr$BlackDists=="Error in file(file, \\rt\\) : cannot open the connection\n",]; sr$romneydists<-as.numeric(as.character(sr$romneydists)) sb<- read.delim(paste(simfolder,st,'/sim',sldl_dists,'/blindSP.txt',sep=''),header=T); sb<-sb[!sb$BlackDists=="Error in file(file, \\rt\\) : cannot open the connection\n",] points(sb$romneydists/sldl_dists, runif(dim(sb)[1],s+0.09,s+0.38) , pch=1,col="gray" ); points(sr$romneydists/sldl_dists, runif(dim(sr)[1],s-0.38,s-0.09) , pch=1,col="blue" ); ##points(sum(e$rsh>0.5)/sldl_dists, s, pch=8,col="red",cex=1.9 ) } legend("top", c("Race-Blind Computer-Simulated House Districting Plans","Race-Conscious Computer-Simulated House Districting Plans") , pch=c(1,1), cex=c(1.2,1.2), pt.cex=c(1,1), col=c("gray","blue"), text.col=c("black","blue"), title.col=c("black") ,bty="y" ) dev.off() ########################## w <- w[order(w$st),] pdf('/mnt/shares/lsa/base/R/Simulations_RepublicanDistricts.pdf',width=8.5,height=6.5) for (s in 1:dim(w)[1]){ st<-w$st[s] files<-list.files(paste(simfolder,st,sep='')); files<-files[regexpr('sim',files)==1][1]; sldl_dists <- as.numeric(substr(files,4,99)); e <- read.delim(paste(simfolder,st,'/sim',sldl_dists,'/enact_dists.txt',sep=''),header=T) sr<- read.delim(paste(simfolder,st,'/sim',sldl_dists,'/raceSP.txt',sep=''),header=T) sb<- read.delim(paste(simfolder,st,'/sim',sldl_dists,'/blindSP.txt',sep=''),header=T) MakeHist(sb$romneydists, paste("Number of Mitt Romney (Republican)-Favoring Districts (",sldl_dists," total districts)",sep=''), sum(e$rsh>0.5),paste(st,": Race-Blind Simulations",sep='')) MakeHist(sr$romneydists, paste("Number of Mitt Romney (Republican)-Favoring Districts (",sldl_dists," total districts)",sep=''), sum(e$rsh>0.5),paste(st,": Race-Conscious Simulations",sep='')) } dev.off() ## st <- c('AL','AR','AZ','CA','DE','FL','GA','IL','LA','MD','MS','NC','NM','NV','NY','SC','TN','TX','VA') w <- as.data.frame(st); w$sfx <- '_sp'; w$sfx[w$st %in% c('CA')]<-'_sp5' w$blind <- as.numeric(lapply(1:dim(w)[1], function(i) length(list.files(paste(simfolder,w$st[i],'/blind',w$sfx[i],sep=''))))); w$race <- as.numeric(lapply(1:dim(w)[1], function(i) length(list.files(paste(simfolder,w$st[i],'/race',w$sfx[i],sep=''))))); w <- w[order(w$st),] pdf('/mnt/shares/lsa/base/R/Simulations_byDistrict_BlackShareOfMajPty.pdf',width=8.5,height=11) for (s in 1:dim(w)[1]){ st<-w$st[s]; print(st) files<-list.files(paste(simfolder,st,sep='')); files<-files[regexpr('sim',files)==1][1]; sldl_dists <- as.numeric(substr(files,4,99)); e <- read.delim(paste(simfolder,st,'/sim',sldl_dists,'/enact_dists.txt',sep=''),header=T) e$right<-e$BRvotes+e$HRvotes+e$ORvotes; e$left<-e$BDvotes+e$HDvotes+e$ODvotes; e$romshare<-e$right/(e$right+e$left); e$BshDems<-e$BDvotes/e$left; e$BshReps<-e$BRvotes/e$right; e$BshMajPty<-e$BshDems; e$BshMajPty[e$right>e$left]<-e$BshReps[e$right>e$left]; res <- read.delim(paste(simfolder,st,'/sim',sldl_dists,'/blindSP.BshMajPty.txt',sep=''), header=F) m<-rbind(res,e$BshMajPty); par(mar=c(6.0,16.3,3,1.5)) plot(sort(e$BshMajPty),1:dim(e)[1],main=paste('\n',st,': ',sldl_dists,' districts (Race-Blind Simulations)',sep=''),ylim=c(dim(res)[1]/60,1.04*dim(e)[1]),xlim=c(min(m),max(m)),col="red",pch=8,yaxt="n",ylab="",xlab="") lines(c(0.5,0.5),c(-100,1000),lty="dashed",col="gray") for(i in 1:dim(e)[1]){lines(c(-1,2),c(i,i),lty="dashed",col="gray",lwd=1);} for(i in 1:dim(res)[1]){points(sort(res[i,]),1:dim(e)[1]+runif(dim(e)[1],-0.2,0.2),cex=0.5,col="gray")} points(sort(e$BshMajPty),1:dim(e)[1],col="red",pch=8) title(xlab="African-American Share of the Voters Supporting the\nMajority Party in the District", cex.lab=1.0,mgp=c(3.0, 1.70, 0)) yl <- ( paste('District with the ',seq(from=5,to=dim(e)[1],by=5),'th-Largest African-American Share',sep='') ); ## axis(2, at=1+dim(e)[1]-seq(from=5,to=dim(e)[1],by=5), cex.axis=0.7, labels=(yl), mgp=c(1.5, 0.6, 0) ,las=1) axis(2, at=dim(e)[1], cex.axis=0.7, labels='District with the Largest African-American Share', mgp=c(1.3, 0.6, 0) ,las=1) legend("top", c(paste(dim(res)[1]," Race-Blind Computer-Simulated House Districting Plans",sep=''),"Enacted House Plan (As of 2016)") , pch=c(1,8), cex=c(0.7,0.7), pt.cex=c(1,1), col=c("gray","red"), text.col=c("black","red"), title.col=c("black") ,bty="y" ) res <- read.delim(paste(simfolder,st,'/sim',sldl_dists,'/raceSP.BshMajPty.txt',sep=''), header=F); res<-res[res$V1!="Error in file(file, \\rt\\) : cannot open the connection\n",]; for(i in 1:dim(res)[2]){res[,i]<-as.numeric(as.character(res[,i]))} m<-rbind(res,e$BshMajPty); par(mar=c(6.0,16.3,3,1.5)) plot(sort(e$BshMajPty),1:dim(e)[1],main=paste('\n',st,': ',sldl_dists,' districts (Race-Conscious Simulations)',sep=''),ylim=c(dim(res)[1]/60,1.04*dim(e)[1]),xlim=c(min(m),max(m)),col="red",pch=8,yaxt="n",ylab="",xlab="") lines(c(0.5,0.5),c(-100,1000),lty="dashed",col="gray") for(i in 1:dim(e)[1]){lines(c(-1,2),c(i,i),lty="dashed",col="gray",lwd=1);} for(i in 1:dim(res)[1]){points(sort(res[i,]),1:dim(e)[1]+runif(dim(e)[1],-0.2,0.2),cex=0.5,col="gray")} points(sort(e$BshMajPty),1:dim(e)[1],col="red",pch=8) title(xlab="African-American Share of the Voters Supporting the\nMajority Party in the District", cex.lab=1.0,mgp=c(3.0, 1.70, 0)) yl <- ( paste('District with the ',seq(from=5,to=dim(e)[1],by=5),'th-Largest African-American Share',sep='') ); ## axis(2, at=1+dim(e)[1]-seq(from=5,to=dim(e)[1],by=5), cex.axis=0.7, labels=(yl), mgp=c(1.5, 0.6, 0) ,las=1) axis(2, at=dim(e)[1], cex.axis=0.7, labels='District with the Largest African-American Share', mgp=c(1.3, 0.6, 0) ,las=1) legend("top", c(paste(dim(res)[1]," Race-Blind Computer-Simulated House Districting Plans",sep=''),"Enacted House Plan (As of 2016)") , pch=c(1,8), cex=c(0.7,0.7), pt.cex=c(1,1), col=c("gray","red"), text.col=c("black","red"), title.col=c("black") ,bty="y" ) } dev.off() pdf('/mnt/shares/lsa/base/R/Simulations_byDistrict_HispanicShareOfMajPty.pdf',width=8.5,height=11) for (s in 1:dim(w)[1]){ st<-w$st[s]; print(st) files<-list.files(paste(simfolder,st,sep='')); files<-files[regexpr('sim',files)==1][1]; sldl_dists <- as.numeric(substr(files,4,99)); e <- read.delim(paste(simfolder,st,'/sim',sldl_dists,'/enact_dists.txt',sep=''),header=T) e$right<-e$BRvotes+e$HRvotes+e$ORvotes; e$left<-e$BDvotes+e$HDvotes+e$ODvotes; e$romshare<-e$right/(e$right+e$left); e$HshDems<-e$HDvotes/e$left; e$HshReps<-e$HRvotes/e$right; e$HshMajPty<-e$HshDems; e$HshMajPty[e$right>e$left]<-e$HshReps[e$right>e$left]; res <- read.delim(paste(simfolder,st,'/sim',sldl_dists,'/blindSP.HshMajPty.txt',sep=''), header=F) m<-rbind(res,e$HshMajPty); par(mar=c(6.0,16.3,3,1.5)) plot(sort(e$HshMajPty),1:dim(e)[1],main=paste('\n',st,': ',sldl_dists,' districts (Race-Blind Simulations)',sep=''),ylim=c(dim(res)[1]/60,1.04*dim(e)[1]),xlim=c(min(m),max(m)),col="red",pch=8,yaxt="n",ylab="",xlab="") lines(c(0.5,0.5),c(-100,1000),lty="dashed",col="gray") for(i in 1:dim(e)[1]){lines(c(-1,2),c(i,i),lty="dashed",col="gray",lwd=1);} for(i in 1:dim(res)[1]){points(sort(res[i,]),1:dim(e)[1]+runif(dim(e)[1],-0.2,0.2),cex=0.5,col="gray")} points(sort(e$HshMajPty),1:dim(e)[1],col="red",pch=8) title(xlab="Hispanic Share of the Voters Supporting the\nMajority Party in the District", cex.lab=1.0,mgp=c(3.0, 1.70, 0)) yl <- ( paste('District with the ',seq(from=5,to=dim(e)[1],by=5),'th-Largest Hispanic Share',sep='') ); ## axis(2, at=1+dim(e)[1]-seq(from=5,to=dim(e)[1],by=5), cex.axis=0.7, labels=(yl), mgp=c(1.5, 0.6, 0) ,las=1) axis(2, at=dim(e)[1], cex.axis=0.7, labels='District with the Largest Hispanic Share', mgp=c(1.3, 0.6, 0) ,las=1) legend("top", c(paste(dim(res)[1]," Race-Blind Computer-Simulated House Districting Plans",sep=''),"Enacted House Plan (As of 2016)") , pch=c(1,8), cex=c(0.7,0.7), pt.cex=c(1,1), col=c("gray","red"), text.col=c("black","red"), title.col=c("black") ,bty="y" ) res <- read.delim(paste(simfolder,st,'/sim',sldl_dists,'/raceSP.HshMajPty.txt',sep=''), header=F); res<-res[res$V1!="Error in file(file, \\rt\\) : cannot open the connection\n",]; for(i in 1:dim(res)[2]){res[,i]<-as.numeric(as.character(res[,i]))} m<-rbind(res,e$HshMajPty); par(mar=c(6.0,16.3,3,1.5)) plot(sort(e$HshMajPty),1:dim(e)[1],main=paste('\n',st,': ',sldl_dists,' districts (Race-Conscious Simulations)',sep=''),ylim=c(dim(res)[1]/60,1.04*dim(e)[1]),xlim=c(min(m),max(m)),col="red",pch=8,yaxt="n",ylab="",xlab="") lines(c(0.5,0.5),c(-100,1000),lty="dashed",col="gray") for(i in 1:dim(e)[1]){lines(c(-1,2),c(i,i),lty="dashed",col="gray",lwd=1);} for(i in 1:dim(res)[1]){points(sort(res[i,]),1:dim(e)[1]+runif(dim(e)[1],-0.2,0.2),cex=0.5,col="gray")} points(sort(e$HshMajPty),1:dim(e)[1],col="red",pch=8) title(xlab="Hispanic Share of the Voters Supporting the\nMajority Party in the District", cex.lab=1.0,mgp=c(3.0, 1.70, 0)) yl <- ( paste('District with the ',seq(from=5,to=dim(e)[1],by=5),'th-Largest Hispanic Share',sep='') ); ## axis(2, at=1+dim(e)[1]-seq(from=5,to=dim(e)[1],by=5), cex.axis=0.7, labels=(yl), mgp=c(1.5, 0.6, 0) ,las=1) axis(2, at=dim(e)[1], cex.axis=0.7, labels='District with the Largest Hispanic Share', mgp=c(1.3, 0.6, 0) ,las=1) legend("top", c(paste(dim(res)[1]," Race-Blind Computer-Simulated House Districting Plans",sep=''),"Enacted House Plan (As of 2016)") , pch=c(1,8), cex=c(0.7,0.7), pt.cex=c(1,1), col=c("gray","red"), text.col=c("black","red"), title.col=c("black") ,bty="y" ) } dev.off() ###############################################