Announcement

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

  • How to add a box with descriptive info (#events, mean, etc) to a histogram

    Hi everyone,

    First time poster here. I would like to have my histograms automatically display information like the total number of counts, mean, etc (actually just those two would be fine). I used to use something called ROOT which did this automatically, but I can't seem to find any information online about any kind of option that would do this. I don't need to overlay a normal distribution or anything like that - I just want numbers.

    I am using Stata 11 on a mac.

    Thanks in advance for any help,
    Cara

  • #2
    What about this?

    Code:
    // open example data
    sysuse auto
    
    // collect the mean and N
    sum price
    local n = r(N)
    local m : display %5.0fc r(mean) // gives the mean a nice format
    
    // graph the histogram
    hist price, note("N = `n'" "mean = `m'")
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Hi Maarten,

      Thanks so much for your help! That did work, and led me to my final solution. In the end I used a text box and I'm including the code below in case anyone does a search for "Display Statistics within Histogram".

      Cheers,
      Cara


      // collect the mean and N
      sum vitD if genderID==1
      local n = r(N)
      local m : display %5.0fc r(mean) // gives the mean a nice format

      sum vitD if genderID==0
      local k = r(N)
      local j : display %5.0fc r(mean)

      twoway (histogram vitD if genderID==1, frequency width(2) color(gs11) lwidth(none)) ///
      (histogram vitD if genderID==0, frequency width(2) color(none) lwidth(medium) lcolor(navy) ///
      , title("Vitamin D Level Distribution by Gender") ///
      text(33 28 "Females: N = `n', mean = `m'" "Males: N = `k', mean = `j'" ///
      , place(e) box just(left) margin(l+4 t+1 b+1) width(50) ) ///
      legend(ring(0) pos(1) order(2 "Male" 1 "Female")) ///
      name(histoVitDGender, replace) )

      Comment


      • #4
        I have tried the following to get also the SD and the Median.

        // collect the mean and N
        sum salary

        local n = r(N)
        local m : display %5.0fc r(mean)
        local l : display %5.0fc r(p50)
        local k : display %5.0fc r(sd)

        // graph the histogram
        hist salary if male == 0 & fulltime == 1, xline(656) frequency normal note("N = `n'" "Mean = `m'" "Median = `l'" "SD = `k'")


        For the SD it works, but the median is blank.

        Can anyone spot my mistake?

        Attached Files

        Comment


        • #5
          You need the detail option of summarize to get the median too.

          Comment

          Working...
          X