# gelman_timeuse.R # Graphics examples from Andrew Gelman: # http://www.stat.columbia.edu/~cook/movabletype/archives/2011/04/the_r_code_for.html # April 2011 # # The data a1 <- c(4.2,3.2,11.1,1.3,2.2,2.0) a2 <- c(3.9,3.2,10.0,0.8,3.1,3.1) a3 <- c(6.3,2.5,9.8,0.9,2.2,2.4) a4 <- c(4.4,3.1,9.8,0.8,3.3,2.7) a5 <- c(4.8,3.0,9.9,0.7,3.3,2.4) a6 <- c(4.0,3.4,10.5,0.7,3.3,2.1) a <- rbind(a1,a2,a3,a4,a5,a6) avg <- colMeans (a) avg.array <- t (array (avg, rev(dim(a)))) diff <- a - avg.array country.name <- c("France", "Germany", "Japan", "Britain", "USA", "Turkey") # The line plots par (mfrow=c(2,3), mar=c(4,4,2,.5), mgp=c(2,.7,0), tck=-.02, oma=c(3,0,4,0), bg="gray96", fg="gray30") for (i in 1:6){ plot (c(1,6), c(-1,1.7), xlab="", ylab="", xaxt="n", yaxt="n", bty="l", type="n") lines (1:6, diff[i,], col="blue") points (1:6, diff[i,], pch=19, col="black") if (i>3){ axis (1, c(1,3,5), c ("Work,\nstudy", "Eat,\nsleep", "Leisure"), mgp=c(2,1.5,0), tck=0, cex.axis=1.2) axis (1, c(2,4,6), c ("Unpaid\nwork", "Personal\nCare", "Other"), mgp=c(2,1.5,0), tck=0, cex.axis=1.2) } else{ axis (1, c(1,3,5), c ("Work", "Eat,sleep ", " Leisure"), mgp=c(2,.5,0), tck=0, cex.axis=1.2) axis (1, c(2,4,6), c ("Unpaid ", " P.care", "Other"), mgp=c(2,.5,0), tck=0, cex.axis=1.2) } if (i%%3==1) mtext ("Excess or deficit Hours/day", 2, 2.5, cex=.9, col="black") axis (2, c(-1,0,1), c("-1 hr", "Avg", "+1 hr"), cex.axis=1.3) mtext (country.name[i], 3, -1.5, col="black", cex=.9) abline (0, 0, col="gray") } mtext ("Excess or deficit hours/day spent at each activity (compared to avg country)", side=3, outer=TRUE, line=2, col="black") mtext ("Redrawing of graph from the Economist, http://www.economist.com/blogs/dailychart/2011/04/time_use, using line rather than circle plots to facilitate comparisons within and between countries", side=1, line=1.5, outer=TRUE, cex=.7, col="black") # The bar plots (these are cool) par (mfrow=c(1,7), mar=c(1,0,1,0), mgp=c(1,.5,0), tck=-.04, oma=c(3,0,4,0), bg="gray96", fg="gray30") plot (c(0,1),c(.5,6.5), xlab="", ylab="", xaxt="n", yaxt="n", bty="n", type="n") labels <- c ("Work, study", "Unpaid work", "Eat, sleep", "Pers. care", "Leisure", "Other") text (0, 6:1, labels, adj=0, col="black", cex=1.4) for (i in 1:6){ plot (c(-1.2,2.2), c(.5,6.5), xlab="", ylab="", xaxt="n", yaxt="n", bty="n", type="n") abline (v=0, col="gray") for (j in 6:1){ lines (c(0, diff[i,7-j]), c(j,j), lwd=3, col="blue") } axis (1, c(-1,0,1), c("-1 hr", "Av", "+1 hr"), cex.axis=1.05) mtext (paste(country.name[i]," "), 3, .5, col="black", cex=.9) } mtext ("Excess or deficit hours/day spent at each activity (compared to avg country)", side=3, outer=TRUE, line=2, col="black", cex=.9) mtext ("Redrawing of graph from the Economist, http://www.economist.com/blogs/dailychart/2011/04/time_use", side=1, line=1.5, outer=TRUE, cex=.7, col="black")