,

Overlaying histograms in Stata

For analysing data and comparing distributions, I often want to overlay two histograms. Without further options, however, one distribution usually overlays the other and makes comparisons cumbersome. It is possible to set a few options to make the figure look nice. twoway (hist VAR1, frac lcolor(gs12) fcolor(gs12)) (hist VAR2, frac fcolor(none) lcolor(red)), legend(off) xtitle(“VAR1 (red:…

For analysing data and comparing distributions, I often want to overlay two histograms. Without further options, however, one distribution usually overlays the other and makes comparisons cumbersome. It is possible to set a few options to make the figure look nice.

twoway (hist VAR1, frac lcolor(gs12) fcolor(gs12)) (hist VAR2, frac fcolor(none) lcolor(red)), legend(off) xtitle("VAR1 (red: VAR2)")

In this example, the bars of variable VAR2 have a red border and transparent body, whereas VAR1 is shown as in its default colors (both border and body is set to gs12).

 

Responses to “Overlaying histograms in Stata”

  1. Diana

    Thanks. This is very helpful. Is there a way to overlay a normal curve on top of each histogram. STATA does not seem to like the “normal” option when using twoway

  2. Jan Sauermann

    Ah, I was not aware that this works only without using twoway. I just found this (a bit lengthy) workaround: https://www.stata.com/statalist/archive/2010-10/msg00819.html
    I hope this helps!

  3. Andrew G

    Quite helpful Jan, thank you. I always forget how easy it is to overlay one kind of graph onto another in Stata.

Leave a Reply