Announcement

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

  • Customizing bar labels in hbar graphs

    Hi all,

    I would like to add the "%" percentage sign to the bar labels in this hbar graph. I know how to do this in a twoway graph (which has been discussed in this post), but I cannot figure out if this is possible with an hbar? A sample graph I am working with is below (I have data an at individual level, but have collapsed it into averages before creating the graph). Thanks a lot in advance!

    Code:
    clear
    input byte char float(var_1 var_2 var_3 var_4)
    8 .33 .47 .13  .05
    9 .41 .51 .06 .01
    end
    
            
    graph hbar var_1 var_2 var_3 var_4, over(char, sort(1) descending relabel(1 "Example label 1" 2 "Example label 2") axis(outergap(*30))) percent stack ylab(,labsize(small)) ytitle("") blabel(bar, size(small) color(white) position(center) format(%4.0f)) legend(order(1 "Strongly agree" 2 "Agree" 3 "Disagree" 4 "Strongly disagree") rows(1) position(6) size(small)) xsize(6) ysize(3)
    I'd like to display the labels for the bar graph below as percents (with a "%" sign after them), but don't know how to do this beyond manually adding

  • #2
    You just run the code from the link after your graph command.

    Code:
    clear
    input byte char float(var_1 var_2 var_3 var_4)
    8 .33 .47 .13  .05
    9 .41 .51 .06 .01
    end
    
            
    graph hbar var_1 var_2 var_3 var_4, over(char, sort(1) descending relabel(1 "Example label 1" 2 "Example label 2") axis(outergap(*30))) percent stack ylab(,labsize(small)) ytitle("") blabel(bar, size(small) color(white) position(center) format(%4.0f)) legend(order(1 "Strongly agree" 2 "Agree" 3 "Disagree" 4 "Strongly disagree") rows(1) position(6) size(small)) xsize(6) ysize(3)
    local nb=`.Graph.plotregion1.barlabels.arrnels'
    forval i=1/`nb' {
      di "`.Graph.plotregion1.barlabels[`i'].text[1]'"
      .Graph.plotregion1.barlabels[`i'].text[1]="`.Graph.plotregion1.barlabels[`i'].text[1]'%"
    }
    .Graph.drawgraph

    Comment


    • #3
      My answer on adding % signs is trivial, but given the nature of the data, I wanted to show what floatplot from SSC would do.

      Neither set of proportions adds to exactly 1; hence some minor rescaling.


      Code:
      clear
      input byte char float(var_1 var_2 var_3 var_4)
      8 .33 .47 .13  .05
      9 .41 .51 .06 .01
      end
      
              
      graph hbar var_1 var_2 var_3 var_4, ///
      over(char, sort(1) descending relabel(1 "Example label 1" 2 "Example label 2") axis(outergap(*30))) ///
      percent stack ylab(,labsize(small)) ytitle("") ///
      blabel(bar, size(small) color(white) position(center) format(%4.0f)) ///
      legend(order(1 "Strongly agree" 2 "Agree" 3 "Disagree" 4 "Strongly disagree") rows(1) position(6) size(small)) ///
      xsize(6) ysize(3) name(G1, replace) yla(0 "0%" 20 "20%" 40 "40%" 60 "60%" 80 "80%" 100 "100%")
      
      reshape long var_, i(char) j(answer)
      
      replace answer = 5 - answer 
      
      label def answer 4 "Strongly agree" 3 "Agree" 2 "Disagree" 1 "Strongly disagree"
      label val answer answer 
      
      label def char 8 "Example label 1" 9 "Example label 2"
      label val char char 
      
      floatplot answer [aw=var_], over(char) highnegative(2) vertical  fcolors(stc2 stc2*0.5 stc1*0.5 stc1) subtitle(%) name(G2, replace)
      Click image for larger version

Name:	float_G1.png
Views:	1
Size:	26.7 KB
ID:	1747388

      Click image for larger version

Name:	float_G2.png
Views:	1
Size:	34.4 KB
ID:	1747389

      Comment

      Working...
      X