Announcement

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

  • two histograms-one plot. no labels on second hist

    Hi,

    I'm looking to add height labels to two sets of histogram bars placed on one plot

    using addplot

    an explanation of my problem:

    sysuse auto
    hist foreign, density addplot(histogram headroom) addlabel

    I'd like the height labels on the headroom variable as well as on foreign. Perhaps I should be using graph bar?

    Thanks in
    advance for any help!

    Josh



  • #2
    The problem here is that your main call is to histogram, but the addplot() call is to twoway histogram, which doesn't have an addlabel option.

    One way of doing this is to drill down and create your own variables.

    Code:
    sysuse auto, clear
    set scheme s1color
    twoway__histogram_gen foreign, gen(d_f x1) width(0.5) discrete
    twoway__histogram_gen headroom, gen(d_h x2) width(0.5)
    
    gen text1 = string(d_f, "%3.2f")
    gen text2 = string(d_h, "%3.2f")
    
    twoway bar d_f x1, barw(0.5) ||                    ///
    bar d_h x2, barw(0.5) yla(, ang(h)) ||             ///
    scatter d_f x1, ms(none) mla(text1) mlabpos(12) || ///
    scatter d_h x2, ms(none) mla(text2) mlabpos(12)    ///
    legend(order(1 "`: var label foreign'" 2 "`: var label headroom'"))
    Here is the result. For more on twoway__histogram_gen, see

    SJ-5-2 gr0014 . . . . . . . Stata tip 20: Generating histogram bin variables
    . . . . . . . . . . . . . . . . . . . . . . . . . . . . D. A. Harrison
    Q2/05 SJ 5(2):280--281 (no commands)
    tip illustrating the use of twoway__histogram_gen for
    creation of complex histograms and other graphs or tables





    .

    Comment

    Working...
    X