Announcement

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

  • Bar graph with groups in x axis and percentage within the group in y axis

    Hi,
    I tried to do a bar figure with 4 groups and to check how many are very happy in each group.
    Right now it is not good because I know that in group 1 there are much more observations and that the bar for group 1 is very high relative to the others because of this. Inside group 1 the percentage suppose to be around 30%.

    So, can I check the percentage in each group alone? How can I make a bar graph with groups in x axis and percentage within the group in y axis?
    I tried to find in stata's guide or here in the forum but I didn't find it.

    My code now:
    Code:
    graph bar (percent) vhappy, over(career_married), if female==1 & college==1
    The graph now:
    Click image for larger version

Name:	NOT GOOD.jpg
Views:	1
Size:	14.6 KB
ID:	1654150



    Thank you!

  • #2
    If vhappy is 0 or 1 then what you want is more likely to be something like


    Code:
    graph bar (mean) vhappy, yla(0 0.1 "10" 0.2 "20" 0.3 "30") ytitle(percent very happy) over(carrier_married)
    Last edited by Nick Cox; 13 Mar 2022, 16:08.

    Comment


    • #3
      To understand what is happening, run the following.

      Code:
      . sysuse auto, clear
      (1978 automobile data)
      
      . graph bar (mean) foreign, over(rep78)
      
      . tabstat foreign, by(rep78) s(mean)
      
      Summary for variables: foreign
      Group variable: rep78 (Repair record 1978)
      
         rep78 |      Mean
      ---------+----------
             1 |         0
             2 |         0
             3 |        .1
             4 |        .5
             5 |  .8181818
      ---------+----------
         Total |  .3043478
      --------------------
      
      . graph bar (mean) foreign, over(rep78) yla(0 "0" 0.25 "25" 0.5 "50" 0.75 "75" 1 "100") ytitle(% foreign)
      
      . graph bar (percent) foreign, over(rep78) ytitle(% observations)
      
      
      . tab rep78
      
           Repair |
      record 1978 |      Freq.     Percent        Cum.
      ------------+-----------------------------------
                1 |          2        2.90        2.90
                2 |          8       11.59       14.49
                3 |         30       43.48       57.97
                4 |         18       26.09       84.06
                5 |         11       15.94      100.00
      ------------+-----------------------------------
            Total |         69      100.00
      
      . graph bar (percent) foreign, over(rep78) ytitle(% observations) yli(2.9 11.59 43.49 26.09 15.94)
      
      .

      Comment


      • #4
        Thank you Nick.
        I didn't think about the fact that "very happy" is a binary variable.

        Comment


        • #5
          I was guessing that vhappy is binary, but you seem to be confirming it.

          Comment

          Working...
          X