Announcement

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

  • Position of bar label

    I am using the following command to generate a horizontal stacked bar graph:
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte event float(level1 level2 level3 level4 level5 level6 level7 level8)
    1 2558    .   .  .   .   .   .   .
    2    . 1257 227 76   .   .   .   .
    3    .    .   .  . 985 272   .   .
    8    .    .   .  .   .   . 933   .
    9    .    .   .  .   .   .   . 425
    end
    label values event event
    label def event 1 "Study Event 1", modify
    label def event 2 "Study Event 2", modify
    label def event 3 "Study Event 3", modify
    label def event 8 "Study Event 8", modify
    label def event 9 "Study Event 9", modify
    
    graph hbar (asis) level*, ///
        over(event)  stack ///
        blabel(bar, size(vsmall) position(inside)) ///
        ytitle(Count) name(totals, replace)  legend(off) ///
        bar(1, color(ltblue)) bar(2, color(cranberry)) bar(3, color(forest_green)) ///
        bar(4, color(gray%25)) bar(5, color(khaki)) bar(6, color(olive%50)) ///
        bar(7, color(pink)) bar(8, color(teal))
    But I'm having a problem with the bar placement. According to the Stata documentation, specifying -position(inside)- will cause the label to appear at the right end of the corresponding bar. And for most of the bars, it does that. But there is one bar in the graph, a very short one, that is barely large enough to contain the label. And for that one, the label shows up left-justified in its bar, or, with some data inputs it ends up concatenated with the label in the bar immediately to its left. (Look at the rightmost bar segment for "Event 2" in the output above.

    I'm able to patch up that bar using the Graph Editor, but I'm wondering if there's some way I can modify the code to make that manual step unnecessary.

    (Please do not comment on the color scheme. I know it's garish.)




  • #2
    You can use the -gap()- suboption to adjust the distance from the bar edge.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte event float(level1 level2 level3 level4 level5 level6 level7 level8)
    1 2558    .   .  .   .   .   .   .
    2    . 1257 227 76   .   .   .   .
    3    .    .   .  . 985 272   .   .
    8    .    .   .  .   .   . 933   .
    9    .    .   .  .   .   .   . 425
    end
    label values event event
    label def event 1 "Study Event 1", modify
    label def event 2 "Study Event 2", modify
    label def event 3 "Study Event 3", modify
    label def event 8 "Study Event 8", modify
    label def event 9 "Study Event 9", modify
    
    graph hbar (asis) level*, ///
        over(event)  stack ///
        blabel(bar, size(vsmall) position(inside) gap(0)) ///
        ytitle(Count) name(totals, replace)  legend(off) ///
        bar(1, color(ltblue)) bar(2, color(cranberry)) bar(3, color(forest_green)) ///
        bar(4, color(gray%25)) bar(5, color(khaki)) bar(6, color(olive%50)) ///
        bar(7, color(pink)) bar(8, color(teal))
    Res.:

    Click image for larger version

Name:	totals.png
Views:	1
Size:	31.6 KB
ID:	1784315


    Comment


    • #3
      Excellent! Thank you very much.

      Comment

      Working...
      X