Announcement

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

  • Adding mean of variables in boxplot

    Hello Stata users,

    I want to add the mean of two variables (meanEI EER) in the boxplot (see attached gragh). My code for the box plot is:

    Code:
    graph box meanEI EER, ///
        title("Graph 4.1: Mean energy intake and estimated energy requirement", size(medium)) ///
        subtitle("Infants and young children under 96 months",size(medsmall)) ///
        ytitle ("Kcal", size(medsmall) margin(medium)) ///
        ylab(500 1000 1500 2000 2500 3000, angle(horizontal)) ///
        box(1,  color(navy8)) marker(1, mcolor(navy8) msize(small)) ///
        box(2,  color(green)) marker(2, mcolor(green) msize(small)) ///
        intensity(75) lintensity(100) graphregion(color(white)) ///
        scheme(s2color) legend(order( 1 "Mean EI (Kcal/d)" 2 "EER (Kcal/d)")) ///
        note("EI = energy intake; EER = estimated energy requirement")
    I read that the scatter option need to be added including new variables for mean:

    Code:
    egen med2 = mean(meanEI)
    egen med3 = mean(EER)
    
    twoway (box meanEI EER) ///
        (scatter med2 med3) ///
        title("Graph 4.1: Mean energy intake and estimated energy requirement", size(medium)) ///
        subtitle("Infants and young children under 96 months",size(medsmall)) ///
        ytitle ("Kcal", size(medsmall) margin(medium)) ///
        ylab(500 1000 1500 2000 2500 3000, angle(horizontal)) ///
        box(1,  color(navy8)) marker(1, mcolor(navy8) msize(small)) ///
        box(2,  color(green)) marker(2, mcolor(green) msize(small)) ///
        intensity(75) lintensity(100) graphregion(color(white)) ///
        scheme(s2color) legend(order( 1 "Mean EI (Kcal/d)" 2 "EER (Kcal/d)")) ///
        note("EI = energy intake; EER = estimated energy requirement")
    Stata returns an error saying "box is not a twoway plot type".

    Can someone please tell me how I can make it work?

    I am using Stata 12

  • #2
    Please don't attach graphs as photos; attach them as .png attachments. This is explained in the FAQ Advice.

    There are many questions in the forum of this kind. Did you search the forum first? Also, in Stata

    Code:
     
    search box
    will give you clickable links to (e.g.)

    SJ-13-2 gr0039_1 . Speaking Stata: Creating and varying box plots: Correction
    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
    Q2/13 SJ 13(2):398--400 (no commands)
    corrects error in code given


    SJ-9-3 gr0039 . . . . . . . . Speaking Stata: Creating and varying box plots
    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
    Q3/09 SJ 9(3):478--496 (no commands)
    explains how to use egen to calculate the statistical
    ingredients needed for box plots and variations of box
    plots; shows the use of twoway to then create the plots

    Note also stripplot (SSC). For example,

    Code:
    sysuse auto, clear 
    stripplot mpg, cumul cumprob box centre over(foreign) refline vertical xsize(3)
    gives you a box plot, a superimposed quantile plot (more informative than the usual graph with rules based on quartiles +/- 1.5 IQR) and reference lines for the means.

    Click image for larger version

Name:	anotherqbox.png
Views:	1
Size:	8.3 KB
ID:	1303344



    Comment


    • #3
      You can also try this one:

      Code:
      sysuse auto, clear 
      egen median = median(mpg), by(foreign)
      egen upq = pctile(mpg), p(75) by(foreign)
      egen loq = pctile(mpg), p(25) by(foreign)
      egen iqr = iqr(mpg), by(foreign)
      egen upper = max(min(mpg, upq +1.5*iqr)), by(foreign)
      egen lower = min(max(mpg, upq -1.5*iqr)), by(foreign)
      egen mean = mean(mpg), by(foreign)
      
      twoway ///
      || rbar med upq foreign, pstyle(p1) barw(0.35) blc(black) bfc(none) lwidth(medthick) legend(off)  ///
      || rbar med loq foreign, pstyle(p1) barw(0.35) blc(black) bfc(none) lwidth(medthick) legend(off)  ///
      || rspike upq upper foreign, pstyle(p1) lcolor(black) msize(*4) lwidth(medthick) legend(off)  ///
      || rspike loq lower foreign, pstyle(p1) lcolor(black) msize(*4) lwidth(medthick) legend(off)  ///
      || rcap upper upper foreign, pstyle(p1) msize(*2) lcolor(black) msize(*4) lwidth(medthick) legend(off) ///
      || rcap lower lower foreign, pstyle(p1) msize(*2) lcolor(black) msize(*4) lwidth(medthick) legend(off) ///
      || scatter mean foreign , msymbol(D) mcolor(black) msize(small) yscale(range(0 5))legend( label(1 "Data"))  ///
      , legend( label(1 "Data") label(2 "45-degree line") label(3 "45-degree line") label(4 "45-degree line") label(4 "45-degree line") label(6 "45-degree line") label(7 "45-degree line") rows(1))  ///
       yscale(range(0 5)) ///
      yla(, ang(h) ) ytitle("Mileage(mpg)") xtitle("") ///
      xla(1 `" " Foreign" "' 0 "Domestic", noticks)

      Comment


      • #4
        I have a similar problem. While I'm aware of the fact that there are threads covering mean markers in box plots, there's still one problem.

        Since the names of the different variables (/box plots) are quite long I'd like to have them in the legend, with the plot corresponding to each variable having a different color (which is precisely what Delphina Gomes has/had). Any way I can keep (or replicate) that and just add a marker for the mean, Nick Cox?

        Comment

        Working...
        X