#
# 04_makePlots.R
#
# Plot marginals of ANES wide questions from latex table.
#

rm( list=ls() )
if (.Platform$OS == "windows") { 
  # Set working directory in location of script.
  frame_files <- lapply(sys.frames(), function(x) x$ofile)
  frame_files <- Filter(Negate(is.null), frame_files)
  PATH <- dirname(frame_files[[length(frame_files)]])
  setwd(PATH)
}

constant.width <- T # have donor categories constant width.

#
# Load data written from 02_AnalyzeMergedDataSet.do.
#
dat <- read.csv("dataForRPlots.csv")

pdf("figures/FigureS03.pdf",height=11,width=8.5)
# Figure S3: Three frames tabulating faminc2, educ, and decades by iscont_pid.
par.old <- par(mfrow=c(3,1),mar=c(0.6,3.1,3.1,1.1),oma=c(1.1,0,2.1,0))
# Frame 1.
levels(dat$faminc2) <- c("","<$30,000","$30-60,000","$60-100,000",">$100,000")
tab <- xtabs(dat$V103~dat$iscont_pid+dat$faminc2,exclude=c(NA,""))
if (constant.width) tab <- tab/rowSums(tab)
mosaicplot(tab,xlab="",ylab="",main="",color=T,las=1,cex.axis=1.2,off=5)
title(main="Family income",cex.main=1.2)

# Frame 2.
dat$educfac <- factor(gsub(" graduate","",dat$educ),levels=c("No HS","High school","Some college","2-year","4-year","Post-grad"))
tab <- xtabs(dat$V103~dat$iscont_pid+dat$educfac,exclude=c(NA,""))
if (constant.width) tab <- tab/rowSums(tab)
mosaicplot(tab,xlab="",ylab="",main="",color=T,las=1,cex.axis=1.2,off=5)
title(main="Education",cex.main=1.2)

# Frame 3.
dat$age <- with(dat,ifelse(decades < 3,"18-29",ifelse(decades < 5,"30-49",ifelse(decades < 7,"50-60","61+"))))
tab <- xtabs(dat$V103~dat$iscont_pid+dat$age,exclude=c(NA,""))
if (constant.width) tab <- tab/rowSums(tab)
mosaicplot(tab,xlab="",ylab="",main="",color=T,las=1,cex.axis=1.2,off=5)
title(main="Age",cex.main=1.2)
# Outer titles.
title(main="Figure S3: Demographic Comparisons of Contributors and Non-Contributors, by Party",outer=T,cex=3)
title(xlab="Source: Merged CCES/DIME dataset.",outer=T,line=0)
par(par.old)
dev.off()


pdf("figures/FigureS04.pdf",height=11,width=8.5)
# Figure S4: Two frames tabulating race and pew_religimp by iscont_pid.
par.old <- par(mfrow=c(2,1),mar=c(0.6,3.1,3.1,1.1),oma=c(1.1,0,2.1,0))
# Frame 1.
dat$race2 <- ifelse(dat$race %in% c("Middle Eastern","Mixed","Native American","Other"),"Other",as.character(dat$race))
tab <- xtabs(dat$V103~dat$iscont_pid+dat$race2,exclude=c(NA,""))
if (constant.width) tab <- tab/rowSums(tab)
mosaicplot(tab,xlab="",ylab="",main="",color=T,las=1,cex.axis=1.2,off=5)
title(main="Race",cex.main=1.2)

# Frame 2.
tab <- xtabs(dat$V103~dat$iscont_pid+dat$pew_religimp,exclude=c(NA,""))
if (constant.width) tab <- tab/rowSums(tab)
mosaicplot(tab,xlab="",ylab="",main="",color=T,las=1,cex.axis=1.2,off=5)
title(main="Religion",cex.main=1.2)

# Outer titles.
title(main="Figure S4: Race and Religion of Contributors and Non-Contributors, by Party",outer=T,cex=3)
title(xlab="Source: Merged CCES/DIME dataset.",outer=T,line=0)
par(par.old)
dev.off()


pdf("figures/FigureS06.pdf",height=11,width=8.5)
# Figure S6: Three frames tabulating faminc2, educ, and decades by votetype_pid.
par.old <- par(mfrow=c(3,1),mar=c(0.6,3.1,3.1,1.1),oma=c(1.1,0,2.1,0))
# Frame 1.
levels(dat$faminc2) <- c("","<$30,000","$30-60,000","$60-100,000",">$100,000")
tab <- xtabs(dat$V103~dat$votetype_pid+dat$faminc2,exclude=c(NA,""))
if (constant.width) tab <- tab/rowSums(tab)
mosaicplot(tab,xlab="",ylab="",main="",color=T,las=1,cex.axis=1.2,off=5)
title(main="Family income",cex.main=1.2)

# Frame 2.
dat$educfac <- factor(gsub(" graduate","",dat$educ),levels=c("No HS","High school","Some college","2-year","4-year","Post-grad"))
tab <- xtabs(dat$V103~dat$votetype_pid+dat$educfac,exclude=c(NA,""))
if (constant.width) tab <- tab/rowSums(tab)
mosaicplot(tab,xlab="",ylab="",main="",color=T,las=1,cex.axis=1.2,off=5)
title(main="Education",cex.main=1.2)

# Frame 3.
dat$age <- with(dat,ifelse(decades < 3,"18-29",ifelse(decades < 5,"30-49",ifelse(decades < 7,"50-60","61+"))))
tab <- xtabs(dat$V103~dat$votetype_pid+dat$age,exclude=c(NA,""))
if (constant.width) tab <- tab/rowSums(tab)
mosaicplot(tab,xlab="",ylab="",main="",color=T,las=1,cex.axis=1.2,off=5)
title(main="Age",cex.main=1.2)
# Outer titles.
title(main="Figure S6: Demographic Comparisons of Voters and Non-Voters, by Party",outer=T,cex=3)
title(xlab="Source: Merged CCES/DIME dataset.",outer=T,line=0)
par(par.old)
dev.off()


pdf("figures/FigureS07.pdf",height=11,width=8.5)
# Figure S7: Two frames tabulating race and pew_religimp by votetype_pid.
par.old <- par(mfrow=c(2,1),mar=c(0.6,3.1,3.1,1.1),oma=c(1.1,0,2.1,0))
# Frame 1.
dat$race2 <- ifelse(dat$race %in% c("Middle Eastern","Mixed","Native American","Other"),"Other",as.character(dat$race))
tab <- xtabs(dat$V103~dat$votetype_pid+dat$race2,exclude=c(NA,""))
if (constant.width) tab <- tab/rowSums(tab)
mosaicplot(tab,xlab="",ylab="",main="",color=T,las=1,cex.axis=1.2,off=5)
title(main="Race",cex.main=1.2)

# Frame 2.
tab <- xtabs(dat$V103~dat$votetype_pid+dat$pew_religimp,exclude=c(NA,""))
if (constant.width) tab <- tab/rowSums(tab)
mosaicplot(tab,xlab="",ylab="",main="",color=T,las=1,cex.axis=1.2,off=5)
title(main="Religion",cex.main=1.2)

# Outer titles.
title(main="Figure S7: Race and Religion of Voters and Non-Voters, by Party",outer=T,cex=3)
title(xlab="Source: Merged CCES/DIME dataset.",outer=T,line=0)
par(par.old)
dev.off()

