# China leadership project: plot all age x tenure interactions from rank model # Create lineplots with estimated first differences over tenure and age # clipped to the convex hull for each party congress # Chris Adolph rm(list=ls()) library(MASS) library(car) library(tile) # REPLICATORS! Edit these paths to match the location of the results on your computer! # folders pasted together with each element of filenames should lead to the output file folder <- "/projects/China/replication/" filenames <- c("pc12/pc12agesim_model1run10000ties1.csv", "pc13/pc13agesim_model1run10000ties1.csv", "pc14/pc14agesim_model1run10000ties1.csv", "pc15/pc15agesim_model1run10000ties1.csv", "pc16/pc16agesim_model1run10000ties1.csv" ) # Load tiers data tiers <- c( #pc topup toplow midup midlow bottomup bottomlow 12, 1, 9, 10, 272, 273, 410, 13, 1, 6, 7, 175, 176, 285, 14, 1, 7, 8, 190, 191, 320, 15, 1, 9, 10, 191, 192, 342, 16, 1, 11, 12, 198, 199, 356) tiers <- as.data.frame(matrix(tiers,nrow=5, ncol=7, byrow=TRUE)) names(tiers) <- c("pc","topup","toplow","midup","midlow","bottomup","bottomlow") attach(tiers) minx <- maxx <- NULL for (i in 1:length(filenames)) { data <- read.csv(paste(folder,filenames[i],sep=""),header=TRUE) minx <- min(minx,min(data$age)) maxx <- max(maxx,max(data$age)) } ntraces <- 0 tracectr <- 0 minptyvec <- maxptyvec <- NULL for (i in 1:length(filenames)) { data <- read.csv(filenames[i],header=TRUE) if (i==1) { ptylevels <- c(1934, 1944, 1955) minptyvec <- c(minptyvec,min(ptylevels)) maxptyvec <- c(maxptyvec,max(ptylevels)) } if (i==2) { ptylevels <- c(1943, 1952, 1961) minptyvec <- c(minptyvec,min(ptylevels)) maxptyvec <- c(maxptyvec,max(ptylevels)) } if (i==3) { ptylevels <- c(1947, 1957, 1967) minptyvec <- c(minptyvec,min(ptylevels)) maxptyvec <- c(maxptyvec,max(ptylevels)) } if (i==4) { ptylevels <- c(1958, 1965, 1974) minptyvec <- c(minptyvec,min(ptylevels)) maxptyvec <- c(maxptyvec,max(ptylevels)) } if (i==5) { ptylevels <- c(1964, 1970, 1977) minptyvec <- c(minptyvec,min(ptylevels)) maxptyvec <- c(maxptyvec,max(ptylevels)) } ntraces <- ntraces + length(ptylevels) } collectedtraces <- vector("list",ntraces) tracectr <- 0 minptyvec <- maxptyvec <- NULL for (i in 1:length(filenames)) { data <- read.csv(filenames[i],header=TRUE) if (i==1) { ptylevels <- c(1934, 1944, 1955) minptyvec <- c(minptyvec,min(ptylevels)) maxptyvec <- c(maxptyvec,max(ptylevels)) } if (i==2) { ptylevels <- c(1943, 1952, 1961) minptyvec <- c(minptyvec,min(ptylevels)) maxptyvec <- c(maxptyvec,max(ptylevels)) } if (i==3) { ptylevels <- c(1947, 1957, 1967) minptyvec <- c(minptyvec,min(ptylevels)) maxptyvec <- c(maxptyvec,max(ptylevels)) } if (i==4) { ptylevels <- c(1958, 1965, 1974) minptyvec <- c(minptyvec,min(ptylevels)) maxptyvec <- c(maxptyvec,max(ptylevels)) } if (i==5) { ptylevels <- c(1964, 1970, 1977) minptyvec <- c(minptyvec,min(ptylevels)) maxptyvec <- c(maxptyvec,max(ptylevels)) } for (j in 1:length(ptylevels)) { tracectr <- tracectr+1 currage <- data$age[data$ptime==ptylevels[j]] curpctrank <- data$pctrank[data$ptime==ptylevels[j]] curpctlower <- data$pctlower[data$ptime==ptylevels[j]] curpctupper <- data$pctupper[data$ptime==ptylevels[j]] minptylevels <- min(ptylevels) maxptylevels <- max(ptylevels) cutoff <- c(minptylevels-1,1935,1945,1949,maxptylevels) lty <- c("solid","solid","solid","solid") hue <- c(0, .11, .33, .67 ) for (k in 2:length(cutoff)) { if ((ptylevels[j]>cutoff[k-1])&&(ptylevels[j]<=cutoff[k])) { currlty <- lty[k-1] currhue <- hue[k-1] if (min(cutoff[k],max(ptylevels)) == max(cutoff[k-1],min(ptylevels))) { currsat <- 1 } else { currsat <- (0.2 + 0.8*( (min(cutoff[k],max(ptylevels)) - ptylevels[j])/ (min(cutoff[k],max(ptylevels)) - max(cutoff[k-1],min(ptylevels))) ) ) } collectedtraces[[tracectr]] <- lineplot(x=currage, y=curpctrank, ci=list(mark="shaded",levels=0.95), col=hsv(h=currhue,currsat,v=1), lty=currlty, plot=i, alpha=1, lwd=0 ) } } } } collectedpolygonscc <- collectedpolygonssc <- vector("list",length(filenames)) for (i in 1:length(filenames)) { collectedpolygonscc[[i]] <- polygonTile(x=c(minx,maxx,maxx,minx), y=c( 100*(1-((midlow[i]+0.5)/bottomlow[i])), 100*(1-((midlow[i]+0.5)/bottomlow[i])), 100*(1-((midup[i]-0.5)/bottomlow[i])), 100*(1-((midup[i]-0.5)/bottomlow[i])) ), fill="gray80", col=NA, plot=i, clip="on" ) collectedpolygonssc[[i]] <- polygonTile(x=c(minx,maxx,maxx,minx), y=c( 100*(1-((toplow[i]-0.5)/bottomlow[i])), 100*(1-((toplow[i]-0.5)/bottomlow[i])), 100*(1-((topup[i]+0.5)/bottomlow[i])), 100*(1-((topup[i]+0.5)/bottomlow[i])) ), fill="gray50", col=NA, plot=i, clip="on" ) } textA <- textTile(x=c(40,40,41,72), y=c(5,65,96,90), labels=c("ACC","CC","SC","Year joined CCP"), cex=0.4, plot=1:5, col="black" ) text1 <- textTile(x=c(75,77,50,50), y=c(73,49,23,75), labels=c("1923-1935", "1936-1945", "1946-1949", "1950-1974"), cex=0.4, plot=1, col=c("red", "darkorange", "darkgreen", "blue") ) text2 <- textTile(x=c(78,61,80,49), y=c(65,68,55,48), labels=c("1926-1935", "1936-1945", "1946-1949", "1950-1980"), cex=0.4, plot=2, col=c("red", "darkorange", "darkgreen", "blue") ) text3 <- textTile(x=c( 80,52,48), y=c( 74,65,45), labels=c( "1936-1945", "1946-1949", "1950-1980"), cex=0.4, plot=3, col=c( "darkorange", "darkgreen", "blue") ) text4 <- textTile(x=c( 77,60,48), y=c( 80,77,55), labels=c( "1938-1945", "1946-1949", "1950-1980"), cex=0.4, plot=4, col=c( "darkorange", "darkgreen", "blue") ) text5 <- textTile(x=c( 48), y=c( 55), labels=c( "1953-1980"), cex=0.4, plot=5, col=c( "blue") ) # Modify traces to show first diffs, no color meanRank <- c(46.3,50.2,45.95,50.2,47.85) for (i in 1:15) { mod3 <- i%%3 div3 <- trunc(i/3) if ((i>=1)&(i<=3)) collectedtraces[[i]]$y <- collectedtraces[[i]]$y - meanRank[1] if ((i>=4)&(i<=6)) collectedtraces[[i]]$y <- collectedtraces[[i]]$y - meanRank[2] if ((i>=7)&(i<=9)) collectedtraces[[i]]$y <- collectedtraces[[i]]$y - meanRank[3] if ((i>=10)&(i<=12)) collectedtraces[[i]]$y <- collectedtraces[[i]]$y - meanRank[4] if ((i>=13)&(i<=15)) collectedtraces[[i]]$y <- collectedtraces[[i]]$y - meanRank[5] if (mod3==1) { collectedtraces[[i]]$lty <- "solid"} if (mod3==2) { collectedtraces[[i]]$lty <- "dashed"} if (mod3==0) { collectedtraces[[i]]$lty <- "dotted"; collectedtraces[[i]]$lex <- 2} collectedtraces[[i]]$col <- "black" } # Text labels for lines joinLabs <- textTile(labels="Joined CCP in...", x=55, y=40, cex=0.5, alpha=1, plot=1) labels <- c("1934", "1944", "1955", "1943", "1952", "1961", "1947", "1957", "1967", "1958", "1965", "1974", "1964", "1970", "1977" ) textX <- c(73,59,51, 67,53,53, 65,54,60, 62,48,58, 60,48,57 ) textY <- c(25,-10,8, 16.5,5,-16, 29,7,-10, 25,-8,-20, 26,-5,-17 ) plotLab <- c(1,1,1,2,2,2,3,3,3,4,4,4,5,5,5) lineLabs <- vector("list", 15) for (i in 1:15) { lineLabs[[i]] <- textTile(labels=labels[i],x=textX[i], y= textY[i], cex=0.5, alpha=1, plot=plotLab[i]) } # Legend traces baselineLabel <- textTile(labels="Low Party Experience (-1 sd)", x = 67, y = -75, cex= 0.8, clip="off", alpha=1, plot=1 ) baselineLine <- linesTile(x = c(30,40), y = c(-75,-75), lty="dotted", lex=2, clip="off", alpha=1, plot=1 ) robustLabel <- textTile(labels="Avg Party Exp", x = 128.5, y = -75, cex= 0.8, clip="off", alpha=1, plot=1 ) robustLine <- linesTile(x = c(105,115), y = c(-75,-75), lty="dashed", clip="off", alpha=1, plot=1 ) robustLabel2 <- textTile(labels="High Party Exp (+1 sd)", x = 187, y = -75, cex= 0.8, clip="off", alpha=1, plot=1 ) robustLine2 <- linesTile(x = c(156,166), y = c(-75,-75), lty="solid", clip="off", alpha=1, plot=1 ) # Make lineplot trueXmins <- c(44,44,42,42,43) - 1 trueXmaxs <- c(81, 74, 71, 69, 66) + 1 zeroline <- vector("list",5) for (i in 1:5) { zeroline[[i]] <- linesTile(x = c(trueXmins[i],trueXmaxs[i]), y=c(0,0), col="black", lwd=0.5,lex=0.5, alpha=1, plot=i ) } limits <- cbind(trueXmins,trueXmaxs, -30, 50) maxdiff <- max(trueXmaxs - trueXmins) normalizedDiff <- (trueXmaxs - trueXmins)/maxdiff xat1 <- c(50,60,70,80) xat2 <- c(50,60,70) xat3 <- c(50,60,70) xat4 <- c(50,60,70) xat5 <- c(50,60) yat <- c(-30,-20,-10,0,10,20,30,40,50) ylab <- c(" ","-20"," ","0"," ","20"," ","40"," ") tile(collectedtraces, zeroline, lineLabs, joinLabs, baselineLabel,baselineLine,robustLabel,robustLine,robustLabel2,robustLine2, limits=limits, RxC=c(1,5), xaxis=list(at1=xat1, at2=xat2, at3=xat3, at4=xat4, at5=xat5, cex=0.85), xaxistitle=list(labels=c("Age in 1982","Age in 1987","Age in 1992","Age in 1997","Age in 2002"), cex=.6, y=1.15), yaxis=list(at=yat, labels=ylab, add=c(TRUE,FALSE,FALSE,FALSE,FALSE), cex=.85, major=FALSE), yaxistitle=list(type="row",cex=.6,labels=c("Change in \n Rank Percentile"),x=0), plottitle=list(cex=.7, y=0.5, labels=c("12th PC", "13th PC", "14th PC", "15th PC", "16th PC") ), frame=FALSE, width=list(plot=normalizedDiff,yaxistitle=0.15,yaxis.labelspace=-0.50, spacer=0.5), height=list(plotttile=2,xaxisttitle=0.5), output = list(wide=7.5,outfile="age_summarypres4reduce",type="pdf") )