R and Stata code for chart of overlapping histogram-bar plot

I ran a variety of economic experiments in Burkina Faso in 2013, and I wanted to show how the distribution of choices changed (or did not) from the first round of the experiments to the second round of the experiments. I thought an overlapping histogram would be a simple and easy way to show this. I first coded in in Stata, which is quite easy to do.


(I use locals for all the labels etc because I do the graphs in both English and French)

twoway histogram contribpub_mai13, barwidth(50) discrete color(gs4) percent ///
histogram contribpub_aout13 ,  barwidth(35) discrete color(gs10) percent   ///
legend(off) xtitle(“`labx3′”) ytitle(“`labx4′”)   ///
subtitle(“`labx6′ = `labx9′, `labx7′ = `labx8′”, si(small)) ///
title(“`labx53′” “`labx55′”) name(comin1, replace)
graph export “$JTLjeuxoutfld\contribpub2013.png”, replace

I then tried to do the same chart in R, and this is considerably harder because I don’t know how to get the two sets of bars aligned.  Finally I used “space” and set the distance between the bars on the second graph by trial and error.  Turned out pretty well.  But if I change the width of the second set of bars, then I have to trail and error again for all the spaces between them.  I’m sure there is a mathematical formula I could write that would take any width and spread the bars out evenly… next task for me someday.

Here is the R chart and code.


### create some plots
barplot(contribmai.freq, col = “gray”, border =”black”, ylim=c(0,160)  )
barplot(contribaout.freq, col = “white”, border =”black”,
add =TRUE, width=.8, space=(c(.37, .5, .5, .5, .5, .5)),
names.arg = c(“”),
ylab=(“Number of people choosing amount”), xlab=(“Amount in FCFA”),
main=(“Contributions in public goods game”))
mtext(“May 2013 = gray, August 2013 = white”)

About mkevane

Economist at Santa Clara University and Director of Friends of African Village Libraries.
This entry was posted in Politics. Bookmark the permalink.