Announcement

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

  • how to add bar labels with a twoway spinning bar chart

    Hello,
    I'm trying to add labels to the twoway spanning bar chart. I would like to add percentages of expenditure shares, which is a different variable from the ones I used for the chart (pc) on top of each bar. I could find some answers for normal bar charts but not for this kind of spanning chart. so:

    1) how can I add bar labels based on a "pc" variable, which is not used for making the bar chart?

    And also:
    2) how can I get rid of EF9 from the legend?


    Thank you for your help!

    some of my code:

    Code:
    twoway bar EF1 exp_cum , bartype(spanning) base(0) || bar EF2 exp_cum, bartype(spanning) base(0) || bar EF3 exp_cum, bartype(spanning) base(0) || bar EF4 exp_cum, bartype(spanning) base(0) ||  bar EF5 exp_cum, bartype(spanning) base(0) ||  bar EF6 exp_cum, bartype(spanning) base(0) ||  bar EF7 exp_cum, bartype(spanning) base(0)||  bar EF8 exp_cum, bartype(spanning) base(0)||  bar EF9 exp_cum, bartype(spanning) base(0)  ytitle("yearly avergae household energy footprint GJ") xla(0(100)730)
    figure where I want to put on top percentages of expenditure shares (pc based on variable exp)


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(EF exp exp_cum) str22 var13 float(EF1 EF2 EF3 EF4 EF5 EF6 EF7 EF8) byte EF9 float(grouping pc)
      .168634         0         0 "recreation and culture" .168634        .        .         .         .         .        .        . . 9         0
     .3608554 26.146544 26.146544 "clothing and footwear"        . .3608554        .         .         .         .        .        . . 9 3.5836484
     1.058974  32.42573  58.57227 "other"                        .        . 1.058974         .         .         .        .        . . 9  4.444275
    1.4442434  159.6117 218.18398 "food"                         .        .        . 1.4442434         .         .        .        . . 9   21.8764
    1.5882757  433.2895  651.4734 "petrol/diesel"                .        .        .         . 1.5882757         .        .        . . 9  59.38671
    1.6788563   26.1177  677.5911 "electricity"                  .        .        .         .         . 1.6788563        .        . . 9  3.579695
     4.945707  40.68476  718.2759 "firewood"                     .        .        .         .         .         . 4.945707        . . 9  5.576259
     7.159899 4.2545524  722.5305 "charcoal"                     .        .        .         .         .         .        . 7.159899 . 9 .58312947
            .  7.076372  729.6068 ""                             .        .        .         .         .         .        .        . . 9  .9698884
    end
    Attached Files

  • #2
    This is an interesting question. Thanks for the data example.

    Your graph shows a legend but nothing in your code makes it clear how it was produced. My guess has to be that you are using a particular graph scheme and not telling us what that is. Even so, the legend uses values from var13 and nothing in your code that I can see refers to var13 at all. You do say "some of my code" but it's not outrageous to want to see it all.

    The challenge to add labels leaves open what rounding you want (because up to 8 decimal places won't look good). Where to put them is presumably above the middle of the top of the bar except that rule needs a tweak in at least one case.

    Here is some technique. Note how you can write code to write code, that is, build up what you want to show in a loop over the categories.

    That makes the code easier to read and to edit.

    I fixed the typo in "average".

    I imagine you can fiddle with this. The horrible variable name exp_cum disappeared in my version but you'll want something different in its place.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(EF exp exp_cum) str22 var13 float(EF1 EF2 EF3 EF4 EF5 EF6 EF7 EF8) byte EF9 float(grouping pc)
      .168634         0         0 "recreation and culture" .168634        .        .         .         .         .        .        . . 9         0
     .3608554 26.146544 26.146544 "clothing and footwear"        . .3608554        .         .         .         .        .        . . 9 3.5836484
     1.058974  32.42573  58.57227 "other"                        .        . 1.058974         .         .         .        .        . . 9  4.444275
    1.4442434  159.6117 218.18398 "food"                         .        .        . 1.4442434         .         .        .        . . 9   21.8764
    1.5882757  433.2895  651.4734 "petrol/diesel"                .        .        .         . 1.5882757         .        .        . . 9  59.38671
    1.6788563   26.1177  677.5911 "electricity"                  .        .        .         .         . 1.6788563        .        . . 9  3.579695
     4.945707  40.68476  718.2759 "firewood"                     .        .        .         .         .         . 4.945707        . . 9  5.576259
     7.159899 4.2545524  722.5305 "charcoal"                     .        .        .         .         .         .        . 7.159899 . 9 .58312947
            .  7.076372  729.6068 ""                             .        .        .         .         .         .        .        . . 9  .9698884
    end
    
    gen show = string(pc, "%2.1f")
    gen pos = cond(_n != 7, 12, 11)
    gen where = (exp_cum + exp_cum[_n+1])/2
    local baropts bartype(spanning) base(0)
    local lblopts ms(none) mla(show) mlabvpos(pos) mlabc(black)
    local call
    local lgnd
    local J = 1
    set scheme s1color
    
    forval j = 1/8 {
        
        local call `call' || bar EF`j' exp_cum, `baropts' || scatter EF`j' where in `j', `lblopts'
        local lgd `lgd' `J' "`=var13[`j']'"
        local J = 2 * `j' - 1
    }
    
    
    twoway `call' ytitle("yearly average household energy footprint GJ") yla(, ang(h)) xla(0(100)730) ///
    legend(order(`lgd'))

    Click image for larger version

Name:	spanning.png
Views:	1
Size:	26.4 KB
ID:	1508497

    Comment

    Working...
    X