UCLA Academic Technology Services HomeServicesClassesContactJobs
Search

Stata Code Fragment: 
A code fragment for combining a histogram and boxplot in one graph.

use hsb2, clear

* get values for boxplot
summarize write, d
gen f=43             /* set value a little larger than bin with highest frequency */
gen pmin=r(min)
gen p25=r(p25)
gen p50=r(p50)
gen p75=r(p75)
gen pmax=r(max)
gen pmean=r(mean)

* graph histogram and boxplot on same axes 
two (histogram write, start(30) width(5) freq)                  ///
    (rcap pmin pmax f in 1, hor bcolor(dknavy))                 ///
    (rbar p25 p75 f in 1, hor bcolor(dknavy))                   ///      
    (rcap p50 p50 f in 1, hor bcolor(white))                    ///
    (rcapsym pmean pmean f in 1, hor msym(plus) mcolor(white)), ///
    legend(off) xtitle("Writing Score") ytitle("Frequency")
    
* drop variables created for boxplot values
drop f-pmean



How to cite this page

Report an error on this page

UCLA Researchers are invited to our Statistical Consulting Services
We recommend others to our list of Other Resources for Statistical Computing Help
These pages are Copyrighted (c) by UCLA Academic Technology Services


The content of this web site should not be construed as an endorsement of any particular web site, book, or software product by the University of California