Announcement

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

  • Adding labels to only some columns of an histogram

    Dear Statalisters,

    This is a very simple question.

    I am using Stata 14.

    I am doing an histogram with the percentages of a given variable. See next:

    histogram [variable name], percent addl addlabopts(yvarformat(%4.1f)) name([name of the graph], replace)

    But I do not want labels (percentages at just one digit) at the top of all the columns of the histogram; only on the top of some of them. It could at the top of one column in every four; or alternatively, at the top of the first and last column, which turn out the ones corresponding to the higher percentages of the variable.

    Is there any option that I could add either to addl or addlabopts so that the graph is generated with values in only some of the columns.

    Thanks for your attention

    Luis

  • #2
    This is a very simple question.
    Even if true, that doesn't guarantee a simple answer. Also, if you don't give a data example, replies are less likely.

    My guess is that you need to do this all yourself, to produce string labels that are empty when you want them to be suppressed.

    Here is some technique:

    Code:
    sysuse auto, clear
    
    * if your bins aren't simple, you will need to bin yourself, calculate bin midpoints,
    * generate frequencies, percents and labels
    
    gen mpg2 = 2 * floor(mpg/2)
    gen mpg3 = mpg2 + 1
    bysort mpg2 : gen freq = _N
    count if mpg < .
    gen Percent = 100 * freq / r(N)
    gen show = strofreal(Percent, "%2.1f") if mod(mpg2, 4) == 0
    
    * I think it's easiest to feed the numeric percents to -twoway bar- and the labels to -scatter-
    
    twoway bar Percent mpg3, barw(2) base(0) bfcolor(none) ///
    || scatter Percent mpg3, ms(none) mlabel(show) mlabpos(12) ///
    legend(off) xtitle("`: var label mpg'") yla(, ang(h))
    Click image for larger version

Name:	ortiz.png
Views:	1
Size:	19.7 KB
ID:	1487347




    See also

    Code:
    help twoway__histogram_gen
    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


    • #3
      Many thanks, Nick

      It works beautifully

      Best wishes

      Luis Ortiz

      Comment

      Working...
      X