Announcement

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

  • Mean on x axis in histogram

    Hey,

    I would like to make a histogram, also showing the variable mean on the x-axis and eventually combine them into one graph. My code so far looks as follows:

    Code:
    foreach var of varlist p_beef p_beef_star {
    sum `var'
    local mean_`var' = r(mean)        
    }
    
    hist p_beef, xtitle(UV in €) xline(`mean_p_beef') xlabel(0  mean_p_beef "Mean" 20 (20) 100) name(UV, replace)
    
    hist p_beef_star, xtitle(adjusted UV in €)  xlabel(0  mean_p_beef_star  "Mean" 20 (20) 100) name(price, replace)
    
    graph combine UV price
    I realize that I use an incorrect label specifier but how do I correct it?


  • #2
    Your code was along the right lines in using a local macro once, but you needed it twice.

    That said, you'd be lucky if the text "mean" did not get tangled up with other text on the same axis. I would consider putting it on the top axis.

    Here is some other technique. It takes some experimentation to get the numeric choices quite right, so you may need more lines of code than this.


    Code:
    sysuse auto, clear 
    set scheme s1color 
    
    su mpg, meanonly
    local mean = r(mean)
    
    hist mpg, xli(`mean') lcol(blue) fcol(none) start(10) width(2.5) xaxis(1 2) xla(10(5)40, axis(1)) xla(`mean' "mean", axis(2)) freq yla(, ang(h)) ysc(r(-0.7 .)) addplot(scatteri -0.4 `mean', ms(T)) legend(off)
    Click image for larger version

Name:	fulcrum.png
Views:	1
Size:	14.9 KB
ID:	1703875

    Comment

    Working...
    X