Announcement

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

  • Adding Number of Observations Label to Stacked Bar Chart

    Hi all,

    I'm trying to make a stacked bar chart that shows the number of observations within each group. My current code looks something like this:

    graph bar lab risk gov, over(lt) ytitle("Proportions") ylabel(,labsize(small)) stack

    But I would like there to be a label above each bar that shows the number of observations for that particular bar. Using the following lines:

    sort lt
    by lt: count

    I see that there are 679, 678, and 680 for each bar. Is there a way to label this in the chart?

    Many thanks,
    Cora

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(lab gov risk lt)
        .1700374    .7782796             . 1
       .43530655    .2523498 -.00009400777 1
        .2423384  .035211213    -.27288932 2
       .14841408   1.0353048   -.003870568 1
        .3139711   .01862362     -.3500698 2
       .28238416   .07640956        -.2502 2
        .4400993    .0670787 -.00056580675 1
        .4450542   .23286644             . 1
       .12029877   .06753071     -.3343936 1
        .5082648  .008052898  -.0031476584 1
       .51327187   .04179808   -.001943114 2
        .4545794    .1239085    -.24128672 1
        .5322632  .013264816             . 1
        .4979768  .023063127     -.1731898 1
        .3181508   .06416951    -.22944584 3
       .13223591    .7514665    -.08747536 2
         .243767           0     -.3631753 2
        .3984841   .09781377    -.25557485 2
        .2594303    .6841453    -.02333964 1
     .0002424968   1.3886087 -.00016629793 1
    .00020155523   1.4744323             . 1
     .0002928973    1.482304             . 1
       .25241518    .7487788             . 1
        .5009559   .09423032             . 1
       .22404113   .17477986     -.3677354 2
       .24345666           0     -.3581009 1
        .1634283   .10225035     -.3377799 2
       .50319344   .06749732  -.0008835515 1
        .3006188  .008035306    -.26575184 3
       .12657124    .6291607    -.28722692 1
       .53362954           0             . 1
        .4184705   .01314309     -.2038869 1
        .5343853  .015596353             . 1
        .3781637           0     -.3169422 1
       .50177145   .08583656             . 1
        .3970145   .27546874     -.1764103 1
      .025122266    .7652866     -.0876677 1
        .5389655           0             . 1
        .4668964   .10456214    -.17507926 1
       .53362954           0             . 1
       .30558315   .05589489     -.1812089 2
       .04403041    .3396863     -.3529391 2
        .3763588    .4286624             . 1
        .2673638    .7169658             . 1
       .26665908    .4362247    -.22927435 1
         .264844   .10131685     -.2486496 2
        .3497822   .03381969    -.12766492 2
        .4957976   .05695531             . 1
         .454442           0             . 1
        .4964611           0     -.1172438 2
       .14419264   .21328416     -.2944041 3
      .017567163   1.1927178    -.12055212 1
       .23156355           0     -.3671024 1
       .24158622      .26593     -.3394998 2
        .4820203   .04930139    -.06837179 1
             .54           0             . 1
        .2487957   .21417157    -.13990873 1
        .5003201  .036252417     -.1072251 1
       .04380105    1.280126             . 1
        .3979877   .01832301    -.04471144 1
       .18135896    .5702492   -.006220731 1
       .18662126           0     -.1729077 3
        .1840557   .50763726    -.27756977 1
       .41636425           0    -.25866598 2
        .4459521           0    -.02916088 1
        .3315545           0     -.3131432 1
       .10622215    .5709492             . 1
        .2908345  .031559385    -.15997745 2
        .4678009   .04758657    -.15388933 1
        .4751051   .10665444             . 1
       .09704377    .4514135     -.3623746 2
       .27230695    .4651364    -.05074546 1
        .5260047   .03887583             . 1
       .43815535    .0918106    -.23629928 1
        .3128505    .2115928    -.12426014 1
        .4359849    .0975452    -.13636968 1
        .4457981  .036428157    -.17247716 1
        .5343853  .015596353             . 1
        .4035726    .1919202    -.19471768 1
        .5160003   .01278616   -.005513604 2
        .3007862   .07709143    -.27725396 1
        .4477225   .25632626             . 1
        .2314591 .0040478515    -.29092264 2
         .397527  .006521621    -.20152047 1
        .4879967           0             . 1
        .2080003    .9212269             . 1
             .54           0             . 1
        .4816735   .13781506    -.02348751 1
        .4440349           0    -.08713304 1
        .4171881   .16578023    -.14507967 1
        .4358414    .2036193             . 1
       .42539865   .04657824    -.12620062 2
        .3682399    .4724361             . 1
        .3041541    .2263215     -.2994768 1
        .2044367    .7129219  -.0004181319 1
       .44044185   .13565966    -.15355474 1
        .3315545           0     -.3131432 1
        .2187026   .10570543     -.3050115 1
          .47968    .1424138             . 1
         .367069   .28755468     -.2607651 1
    end

  • #2
    With 3 labels, you can use the -text()- option to insert manually. I do 2 below.

    Code:
    set scheme s1mono
    graph bar lab risk gov, over(lt) ytitle("Proportions") ylabel(,labsize(small)) ///
    stack ysc(r(0 0.8)) text(0.75 15 "{it:n =} 679")  text(0.525 50 "{it:n =} 678") ///
    leg(order(1 "lab" 2 "risk" 3 "gov") row(1))
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	17.7 KB
ID:	1698434

    Comment

    Working...
    X