Announcement

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

  • How to label the bars in a bar chart when using the asyvars option

    I generate a bar chart with
    Code:
    graph bar, over(q166a_ORC_graph) asyvars legend(off)
    This looks like this:

    Click image for larger version

Name:	barchart-asyvars_no-labels_image_6045.png
Views:	2
Size:	25.9 KB
ID:	1360116


    I use the asyvars option to colour the bar automatically by the theme and I use legend(off) because I don't want a legend. However I do want the bars labeled like Stata does it perfectly when not using asyvars:
    Click image for larger version

Name:	barchart-no_asyvars_but_labels_image_6046.png
Views:	2
Size:	81.2 KB
ID:	1360117

    So, how can I make Stata to label the bars in a bar chart while using the asyvars option?

    Many thanks,
    Walter

    ps: This question came up in an older post. Please excuse me posting it on the top level again, but the older thread did not have the correct title for this problem.


  • #2
    Note for your thesis, presentation or paper downstream of this thread that those value labels are peppered with punctuation errors. You could gain space by omitting the reason numbers. It's difficult to imagine that those are of use or interest to any reader.

    Value labels at an angle are usually a pain for readers. They suggest to me that you would be much better off with horizontal bars or a horizontal dot chart.

    In terms of your question: I don't know how to get what you want with graph bar. I don't know why you want what you do, perhaps because you want some kind of technicolor dreamcoat effect.

    You can get there with twoway bar and a little work.

    I don't have your data or even a data example as you didn't give one. (See FAQ Advice #12.)

    Here is some data from the help of
    tabplot (itself downloadable after search tabplot and a competitor here, at least in the eyes of its fond author).

    Code:
    clear
    input byte(agegroup health) long freq
    1 1 243
    1 2 789
    1 3 167
    1 4 18
    1 5 6
    2 1 220
    2 2 809
    2 3 164
    2 4 35
    2 5 6
    3 1 147
    3 2 658
    3 3 181
    3 4 41
    3 5 8
    4 1 90
    4 2 469
    4 3 236
    4 4 50
    4 5 16
    5 1 53
    5 2 414
    5 3 306
    5 4 106
    5 5 30
    6 1 44
    6 2 267
    6 3 284
    6 4 98
    6 5 20
    7 1 20
    7 2 136
    7 3 157
    7 4 66
    7 5 17
    end
    label values health health
    label def health 1 "very good", modify
    label def health 2 "good", modify
    label def health 3 "regular", modify
    label def health 4 "bad", modify
    label def health 5 "very bad", modify
    
    preserve
        
    contract health [w=freq], percent(_percent)
    separate _percent, by(health) veryshortlabel
    twoway bar _percent? health, xla(1/5, valuelabel tlcolor(none)) ///
    barw(0.6 ..) legend(off) xsc(titlegap(*8)) yla(, format(%2.0f)) ///
    ytitle(percent)
    
    restore
    Code:
    
    
    Click image for larger version

Name:	technicolor.png
Views:	1
Size:	8.6 KB
ID:	1360151



    Here we make explicit the kinds of things that graph bar would do internally on your behalf, work with a set of percents and then make those into separate variables for each category if they are to be coloured differently.

    Changing the bar colours would then be possible. I haven't explored that, as indeed my personal view is that data like this don't need multicolour treatment.



    Comment


    • #3
      Thanks Nick,

      that's an impressive sample. I can see that graph bar can't do this with the asyvars option.

      Thanks again,
      Walter

      Comment

      Working...
      X