Announcement

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

  • bar with percentage

    Dear All, I was asked this question (https://bbs.pinggu.org/thread-10357344-1-1.html). Suppose that I have the data and code as
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double(v1 id)
      .1812867065354394 1
    .11979404113813726 2
    .15310145083000348 3
    .08029177505430556 4
    .019067285258387193 5
    .015741907801228243 6
    .003593553058542734 7
    end
    
    label value id way
    label define way 1 "自己储蓄投资" 2"子女赡养" 3"社会养老保险" 4 "离退休工资" 5"商业养老保险" 6"配偶亲属支持" 7 "其他方式"
    graph bar (asis) v1, over(id) ytitle("")  ///  
             asyvars ylabel(0(0.02)0.2) ylabel(0 "0.00%" 0.02 "2.00%" 0.04 "4.00%" 0.06 "6.00%"  ///
             0.08 "8.00%" 0.1 "10.00%" 0.12 "12.00%" 0.14 "14.00%" 0.16 "16.00%" 0.18 "18.00%" 0.2 "20.00%",angle(0) nogrid) ///
             title("家庭养老方式选择" ,pos(6)) legend(col(1) pos(1) ring(0) size(small))  ///
             blabel(bar, position(outside) format(%9.4f) color(black)) ///
             scheme(s1mono)
    The graph is
    Click image for larger version

Name:	initial.jpg
Views:	1
Size:	68.0 KB
ID:	1589038

    But the desired one is (with percentage on the top of the bar)
    Click image for larger version

Name:	wanted.jpg
Views:	1
Size:	62.5 KB
ID:	1589039

    Any suggestionsa re highly appreciated.
    Ho-Chuan (River) Huang
    Stata 19.0, MP(4)

  • #2
    Tastes differ, but is there any ambiguity in specifying that the y-axis represents percentage values without having to have percentage signs all over the place? My take is that you get a cleaner graph in this way. Nevertheless, one way is to create a string containing what is wanted and label your variable with this. Then, explicitly include the category names in the legend. The following uses labmask from the Stata Journal, authored by Nick Cox.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double(v1 id)
      .1812867065354394 1
    .11979404113813726 2
    .15310145083000348 3
    .08029177505430556 4
    .019067285258387193 5
    .015741907801228243 6
    .003593553058542734 7
    end
    
    gen id2= string(v1*100, "%9.2f") + "%"
    *findit labmask TO INSTALL
    labmask id, values(id2)
    graph bar (asis) v1, over(id) ytitle("")  ///  
             asyvars ylabel(0(0.02)0.2) ylabel(0 "0.00%" 0.02 "2.00%" 0.04 "4.00%" 0.06 "6.00%"  ///
             0.08 "8.00%" 0.1 "10.00%" 0.12 "12.00%" 0.14 "14.00%" 0.16 "16.00%" 0.18 "18.00%" ///
             0.2 "20.00%",angle(0) nogrid) title("家庭养老方式选择" ,pos(6))   ///
             blabel(name, position(outside) color(black)) legend(col(1) pos(1) ring(0) size(small) ///
             order(1 "自己储蓄投资" 2 "子女赡养" 3 "社会养老保险" 4 "离退休工资" ///
             5 "商业养老保险" 6 "配偶亲属支持" 7 "其他方式")) scheme(s1mono)
    Res.:

    Click image for larger version

Name:	Graph.png
Views:	1
Size:	25.5 KB
ID:	1589043

    Comment


    • #3
      Unsurprisingly perhaps, I agree on every point with Andrew Musau. Who really needs % on every bar label and axis label?

      I'd go further. Evidently the OP is limited to mono printing. Understood, but then the legend bears a great deal of weight. Best just to remove it, which you can do without losing information.

      Here's another take. Sorry, but I don't read Chinese and don't know whether there is a case for swapping #2 and #3.
      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input double(v1 id)
        .1812867065354394 1
      .11979404113813726 2
      .15310145083000348 3
      .08029177505430556 4
      .019067285258387193 5
      .015741907801228243 6
      .003593553058542734 7
      end
      
      
      tokenize  `" "自己储蓄投资" "子女赡养"  "社会养老保险"  "离退休工资" "商业养老保险"  "配偶亲属支持" "其他方式" "'
      forval i = 1/7 {
          label def id `i' `"``i''"', add
      }
      label val id id
      
      label li id
      
      gen v1pc = string(v1*100, "%9.2f")
      
      twoway bar v1 id, horizontal barw(0.8) ysc(reverse) base(0) || scatter id v1, ms(none) mla(v1pc) ytitle("")  ///  
               yla(1/7, ang(h) val noticks) xlabel(none)  xtitle(% of something) xsc(r(0 0.195) alt) ///
               title("家庭养老方式选择" ,pos(6))  scheme(s1mono) legend(off)
      Click image for larger version

Name:	chinese_bar.png
Views:	1
Size:	36.7 KB
ID:	1589056




      In fact, once the value labels are defined, you can get there directly from code like


      Code:
      . gen v1_pc = 100 * v1
      
      . graph hbar (asis) v1_pc, over(id) blabel(bar, format(%3.2f))

      Comment


      • #4
        Dear Andrew, Many thanks for this helpful suggestion.
        Ho-Chuan (River) Huang
        Stata 19.0, MP(4)

        Comment


        • #5
          Dear Nick, Many thanks for this alternative helpful suggestion as well.
          Ho-Chuan (River) Huang
          Stata 19.0, MP(4)

          Comment

          Working...
          X