Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Adding percentiles to boxplots

    Hello,

    I am using this code for boxplots -

    ​​​​​​graph box , ///
    over(month, relabel()) ///
    title () ///)
    ytitle() ///
    b1title() ///
    ylabel() ///
    yscale(range()) ///
    egen mean = mean(), by()///
    scatter Time mean, ms(Dh) msize(*2)

    Is it possible to add markers to the boxplots indicating the actual values for the median, 1st, and 3rd quartiles?

  • #2
    That code wouldn't run if only because you have run three commands into one. (What the means have to do with your question is also a puzzle.)

    Adding numerical values as text to graph box is a little tricky as its options require you to work out x axis positions.

    Another way to proceed is to use stripplot (SSC). It starts from a different premise, an inclination to show all the data. It also allows variations on the Tukeyish rules about showing data points beyond (lower quartile - 1.5 IQR, upper quartile = 1.5 IQR).

    Here is a worked example. The whiskers extend between 5% and 95% percentiles. I find this easier to explain and to defend than the Tukey rule. The help for stripplot gives several pertinent references. (The version on my machine has even more.)


    Code:
    sysuse auto, clear 
    
    foreach p in 25 50 75 { 
        egen pc`p' = pctile(mpg), by(foreign) p(`p') 
    } 
    
    
    gen foreign2 = foreign - 0.07 
    
    stripplot mpg, over(foreign) vertical stack height(0.18) ms(Sh) ///
    box(barw(0.04)) boffset(-0.14) pctile(5) xla(, noticks) yla(, ang(h)) /// 
    addplot(scatter pc* foreign2, ms(none ..) mlabpos(0 ..) mlabel(pc25 pc50 pc75) mlabc(black ..))
    Click image for larger version

Name:	boxplot_numeric_labels.png
Views:	1
Size:	21.6 KB
ID:	1474259


    With other data, you are likely to have do some work to decide on bin width, "bar" height, box plot offset and position of marker labels. No homunculus inside stripplotcan be your thinking graphic designer.

    Comment

    Working...
    X