Announcement

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

  • Bar Graph, two variables

    Dear Statalists,

    I am a beginner in Stata so apologies for this simple question:

    I have two categorical variables (LifeSat, JobS_overall) both measuring satisfaction from 1-7 with respect to life and job.
    I would like to create a bar graph showing for each category both variables together.

    basically the outcome of this graph:

    graph bar, over(JobS_overall)

    but also with LifeSat. Sorry for the bad image but at least I clarified my goal.

    Do you know how to do this?

    Thank you very much!

    Francesco

    Click image for larger version

Name:	porva.png
Views:	1
Size:	23.7 KB
ID:	1545220
    graph bar, over(JobS_overall)

  • #2
    You can graph multiple yvars over a category

    Code:
    sysuse citytemp
    graph bar (percent) heatdd cooldd, over(division)
    If you post an example of your data using dataex (read the FAQ for details) you'll likely get more substantive feedback.

    Comment


    • #3
      Hi Justin,
      thank you for your answer.
      Below there is the data. I added pid for claritiy but it´s not relevant in my opinion.
      In a panel, people were asked to report their satisfaction level with respect to life and job. Both were measured at a scale of 1 to 7.
      I would like a graph like the one I posted in the picture. On the x-axis the 7 scales. On the y-axis the satisfaction rate.
      It´s basically what Stata does for one variable only, and I would like to display both variable together to show the similarities in the distribution of the answer.

      graph bar, over(JobS_overall)

      but including LifeSat. I


      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input byte(JobS_overall LifeSat) long pid
      5 . 10007857
      6 . 10014608
      6 . 10014608
      6 . 10014608
      6 5 10014608
      7 6 10014608
      4 6 10016813
      5 . 10016848
      7 . 10016848
      6 6 10016872
      6 . 10016872
      6 6 10016872
      6 6 10016872
      6 6 10016872
      6 6 10016872
      6 6 10016872
      6 6 10016872
      5 6 10017933
      4 5 10017933
      6 . 10017968
      4 . 10017968
      6 . 10017968
      4 5 10017992
      7 5 10017992
      6 7 10017992
      2 4 10017992
      6 5 10017992
      4 . 10020209
      5 . 10020209
      4 . 10020233
      5 . 10023526
      6 4 10023526
      5 5 10023526
      5 5 10023526
      5 5 10023526
      3 5 10023526
      6 5 10023526
      5 5 10023526
      6 5 10023526
      6 6 10023526
      6 . 10023569
      4 . 10024646
      6 . 10024646
      6 5 10025804
      5 5 10025804
      5 6 10025804
      5 . 10028005
      4 . 10028005
      6 . 10028005
      6 . 10028005
      3 5 10028005
      5 5 10028005
      6 5 10028005
      6 5 10028005
      5 5 10028005
      6 . 10028005
      6 5 10028005
      6 4 10028005
      6 5 10028005
      5 5 10028005
      6 5 10028005
      7 5 10028005
      6 . 10028382
      6 . 10028382
      6 . 10028382
      6 . 10028382
      5 . 10029133
      4 . 10029133
      6 6 10029133
      5 5 10029133
      5 6 10029133
      5 . 10029133
      7 . 10040404
      6 7 10040404
      7 . 10048219
      6 . 10048219
      7 7 10048219
      7 7 10048219
      7 7 10048219
      7 6 10048219
      6 6 10048219
      6 6 10048219
      6 6 10048219
      7 . 10048243
      6 . 10048243
      6 . 10048243
      6 . 10048243
      7 6 10048243
      7 6 10048243
      6 5 10048243
      7 3 10048243
      5 . 10048243
      6 5 10048243
      6 5 10048243
      5 5 10048243
      5 6 10048243
      7 . 10048278
      6 . 10048278
      5 . 10048278
      6 6 10048278
      end
      label values JobS_overall rjbsat
      label def rjbsat 4 "Not satis/dissatis", modify
      label def rjbsat 7 "Completely satis", modify
      label values LifeSat rlfsato
      label def rlfsato 4 "Not satis/dissat", modify
      label def rlfsato 7 "Completely satisfied", modify

      Comment


      • #4
        basically I would like to put both graphs together.

        graph bar, over(JobS_overall) bar(1, fcolor(red))

        graph bar, over(LifeSat) bar(1, fcolor(yellow))

        Click image for larger version

Name:	prova1.png
Views:	1
Size:	28.5 KB
ID:	1545258

        Click image for larger version

Name:	prova2.png
Views:	1
Size:	29.5 KB
ID:	1545259

        Comment


        • #5
          Not sure how you want to handle missing data. Hopefully this is enough to get you started,

          Code:
          tempfile temp 
          
          preserve 
          contract JobS_overall
          egen sh_jobs = pc(_freq)
          drop _freq
          
          ren JobS_overall category 
          save `temp'
          restore 
          
          
          contract LifeSat
          egen sh_life = pc(_freq)
          drop _freq
          
          ren LifeSat category
          merge 1:1 category using `temp'
          
          label var sh_life "Life Satisfaction"
          label var sh_jobs "Jobs Overall"
          
          graph  bar sh_* , over(category)

          Comment


          • #6
            A lot of work to do here in terms of variable labels, consistent and complete value labels, and so on. But this may help:


            Code:
            * Example generated by -dataex-. To install: ssc install dataex
            clear
            input byte(JobS_overall LifeSat) long pid
            5 . 10007857
            6 . 10014608
            6 . 10014608
            6 . 10014608
            6 5 10014608
            7 6 10014608
            4 6 10016813
            5 . 10016848
            7 . 10016848
            6 6 10016872
            6 . 10016872
            6 6 10016872
            6 6 10016872
            6 6 10016872
            6 6 10016872
            6 6 10016872
            6 6 10016872
            5 6 10017933
            4 5 10017933
            6 . 10017968
            4 . 10017968
            6 . 10017968
            4 5 10017992
            7 5 10017992
            6 7 10017992
            2 4 10017992
            6 5 10017992
            4 . 10020209
            5 . 10020209
            4 . 10020233
            5 . 10023526
            6 4 10023526
            5 5 10023526
            5 5 10023526
            5 5 10023526
            3 5 10023526
            6 5 10023526
            5 5 10023526
            6 5 10023526
            6 6 10023526
            6 . 10023569
            4 . 10024646
            6 . 10024646
            6 5 10025804
            5 5 10025804
            5 6 10025804
            5 . 10028005
            4 . 10028005
            6 . 10028005
            6 . 10028005
            3 5 10028005
            5 5 10028005
            6 5 10028005
            6 5 10028005
            5 5 10028005
            6 . 10028005
            6 5 10028005
            6 4 10028005
            6 5 10028005
            5 5 10028005
            6 5 10028005
            7 5 10028005
            6 . 10028382
            6 . 10028382
            6 . 10028382
            6 . 10028382
            5 . 10029133
            4 . 10029133
            6 6 10029133
            5 5 10029133
            5 6 10029133
            5 . 10029133
            7 . 10040404
            6 7 10040404
            7 . 10048219
            6 . 10048219
            7 7 10048219
            7 7 10048219
            7 7 10048219
            7 6 10048219
            6 6 10048219
            6 6 10048219
            6 6 10048219
            7 . 10048243
            6 . 10048243
            6 . 10048243
            6 . 10048243
            7 6 10048243
            7 6 10048243
            6 5 10048243
            7 3 10048243
            5 . 10048243
            6 5 10048243
            6 5 10048243
            5 5 10048243
            5 6 10048243
            7 . 10048278
            6 . 10048278
            5 . 10048278
            6 6 10048278
            end
            label values JobS_overall rjbsat
            label def rjbsat 4 "Not satis/dissatis", modify
            label def rjbsat 7 "Completely satis", modify
            label values LifeSat rlfsato
            label def rlfsato 4 "Not satis/dissatis", modify
            label def rlfsato 7 "Completely satis", modify
            
            su JobS_overall
            bysort JobS_overall : gen y1 =  100 * _N/r(N)
            label var y1 "%"
            
            su LifeSat 
            bysort LifeSat : gen y2 = 100 * _N/r(N)
            label var y2 "%"
            
            twoway bar y1 JobS_overall, barw(0.8) bfcolor(red) name(G1, replace) xla(2/7, valuelabel) yla(0(20)60)
            
            twoway bar y2 LifeSat, barw(0.8) bfcolor(yellow) name(G2, replace) xla(2/7, valuelabel) yla(0(20)60)
            
            graph combine G1 G2, col(1) xcommon
            Click image for larger version

Name:	twobar.png
Views:	1
Size:	18.6 KB
ID:	1545281

            Comment


            • #7
              Thank you very much Justin. This is what I wanted to achieve although the numbers are different.

              Comment


              • #8
                Thank you very much Justin. This is what I wanted to achieve although the numbers are different.
                I think that's because of the missings. Maybe this is closer.

                Code:
                tempfile temp 
                
                preserve 
                drop if missing(JobS_overall)
                contract JobS_overall
                egen sh_jobs = pc(_freq)
                drop _freq
                
                ren JobS_overall category 
                save `temp'
                restore 
                
                drop if missing(LifeSat)
                
                contract LifeSat
                egen sh_life = pc(_freq)
                drop _freq
                
                ren LifeSat category
                merge 1:1 category using `temp'
                
                label var sh_life "Life Satisfaction"
                label var sh_jobs "Jobs Overall"
                
                graph  bar sh_* , over(category)

                Comment


                • #9
                  Thank you very much Justin, it worked perfectly!
                  Thank you too Nick for your help!

                  Comment

                  Working...
                  X