Announcement

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

  • Graph bar for multiple categories

    Hello everyone,
    I am brand new to Stata, so apologies if my question seems easy to some of you.
    I have the following variables:

    School: an encoded variable ranging from 1-13 for different school names
    Jobs: a dummy variable with 0= no job, 1=job
    Sex: a second variable with 1= M, 2= F.

    I want to classify how the school impacts the job search, for males and females. To do this, I want to make two horizontal bar graphs with blue bars for males, red for women, and for every categories of schools: one graph for Job Yes and one graph for Job No.
    I manage to get what I want with this command:
    Code:
    graph hbar (percent), over(Sex) over(School) over(Jobs)
    and I get this "nice" graph:

    Click image for larger version

Name:	Capture.PNG
Views:	1
Size:	24.4 KB
ID:	1337622

    Now, since the orders of magnitude are very different, I don't see anything in the second part of the graph. So I want just to split the two graphs, to make the bars visibles for Job too.
    If I simply do:
    Code:
    graph hbar (percent) if Jobs==1, over(Sex) over(School)
    I get this, which is not exactly what I want cause I don't need all the M F, I just would like a legend as in the previous graph.

    Click image for larger version

Name:	Capture.PNG
Views:	1
Size:	22.9 KB
ID:	1337623

    And if I do
    Code:
    graph hbar (percent) School if Jobs==1, over(Sex)
    , then is completely not what I want:

    Click image for larger version

Name:	Capture.PNG
Views:	1
Size:	11.0 KB
ID:	1337624

    Where am I wrong?
    Thank you to anyone who can help!!

  • #2
    See a recent post here for an explanation of the options below: http://www.statalist.org/forums/forum/general-stata-discussion/general/1336245-loosing-precision-in-y-axis-large-number?p=1336679#post1336679


    Separating sex into two variables gets you the different colors with only one over() variable

    Code:
    separate Sex, by(Sex)
    graph hbar (percent) Sex0 Sex1  , over(School) by(Jobs, xrescale)
    When working with graph bar or hbar, be sure that you are using the correct code, otherwise your percentages will be off.

    ****I just committed this error!
    For example, the above will give the the percent of those with Jobsthat are men/women and have gone to school #. (the row/col percentage), not the percent of those in the whole sample who are m/w & school # & job/no job (cell).
    Last edited by Carole J. Wilson; 26 Apr 2016, 13:47.
    Stata/MP 14.1 (64-bit x86-64)
    Revision 19 May 2016
    Win 8.1

    Comment


    • #3
      To get the percent of those in the whole sample:

      Code:
      separate Sex, by(Sex)
      preserve
      collapse (percent) Sex0 Sex1, by(Jobs School)
      graph hbar (percent) Sex0 Sex1  , over(School) by(Jobs, col(1) xrescale) legend(lab(1 "new label 1") lab(2 "new label 2")) ysize(10)
      restore
      Stata/MP 14.1 (64-bit x86-64)
      Revision 19 May 2016
      Win 8.1

      Comment


      • #4
        catplot (SSC) offers a variety of percent controls.

        Elementary tutorial:

        Code:
        sysuse auto, clear
        ssc inst catplot 
        catplot rep78 foreign, percent
        catplot rep78 foreign, percent(foreign)
        catplot rep78 foreign, percent(rep78)

        Comment


        • #5
          Thank you both for your help, but unfortunately I'm still confused.

          catplot is very temptating to use, as it doesn't need to create new variables. But if I do
          Code:
          catplot Sex School Jobs
          , I get the same result (with frequency instead of %) as with
          [CODE]

          Code:
           
           graph hbar (percent), over(Sex) over(School) over(Jobs)
          Still not what I am looking for.

          On the other hand, separating Sex gives something similar, but you'r right Carole, that's not the correct %, as I need the percentage over the WHOLE number of individuals. And if I do your second suggestion with
          Code:
          col(1) rescale
          I get this, which is still very weird
          Click image for larger version

Name:	Capture.PNG
Views:	1
Size:	12.5 KB
ID:	1337738

          I am puzzled 'cause it should not be so complicated to separate my initials graphs...

          Comment


          • #6
            I am not clear exactly what you want. Why not post your data? You could do this

            Code:
             
            contract Sex Jobs School  
            ssc inst dataex 
            dataex
            to get something to post. See also


            http://www.statalist.org/forums/help#stata

            Comment


            • #7
              Thank you Nick, I'll have a look on how to post my data.
              ​What I want is just the first graph I posted

              but either with a different scalebar on the lower panel (to be able to visualize the data), or two separate graphs for Job & No Job, where percentages are calculated on the entire sample (the sum of ALL the bars Jobs+NoJobs should give 100).

              Not sure if it is more clear.
              If not, I'll try to post my data.
              Thank you very much!!!

              Comment


              • #8
                It will be much easier to answer if we can see your data and I've already explained how to do it. Should take two minutes to learn how.

                Comment


                • #9
                  Thank you Nick. Here you are the code. I am actually not sure anymore that the percentage on the graph I just posted are correct, again.
                  What I really need is as I said, an overview of the WHOLE sample (sum of all the bars=100), classified by Schools and Sex and Jobs Y/N.

                  Code:
                  * Example generated by -dataex-. To install: ssc install dataex
                  clear
                  input byte Sex float Jobs long School byte _freq
                  1 0  1 1
                  1 0  2 1
                  1 0  3 1
                  1 0  4 1
                  1 0  5 1
                  1 0  6 1
                  1 0  7 1
                  1 0  8 1
                  1 0  9 1
                  1 0 10 1
                  1 0 11 1
                  1 0 12 1
                  1 0 13 1
                  1 0  . 1
                  1 1  1 1
                  1 1  2 1
                  1 1  4 1
                  1 1  5 1
                  1 1  8 1
                  1 1  9 1
                  1 1 10 1
                  1 1 11 1
                  1 1 12 1
                  1 1 13 1
                  1 1  . 1
                  1 .  1 1
                  1 .  2 1
                  1 .  3 1
                  1 .  4 1
                  1 .  5 1
                  1 .  6 1
                  1 .  7 1
                  1 .  8 1
                  1 .  9 1
                  1 . 10 1
                  1 . 11 1
                  1 . 12 1
                  1 . 13 1
                  1 .  . 1
                  2 0  1 1
                  2 0  2 1
                  2 0  3 1
                  2 0  4 1
                  2 0  5 1
                  2 0  6 1
                  2 0  7 1
                  2 0  8 1
                  2 0  9 1
                  2 0 10 1
                  2 0 11 1
                  2 0 12 1
                  2 0 13 1
                  2 0  . 1
                  2 1  1 1
                  2 1  2 1
                  2 1  3 1
                  2 1  4 1
                  2 1  5 1
                  2 1  6 1
                  2 1  8 1
                  2 1  9 1
                  2 1 10 1
                  2 1 11 1
                  2 1 12 1
                  2 1 13 1
                  2 1  . 1
                  2 .  1 1
                  2 .  2 1
                  2 .  3 1
                  2 .  4 1
                  2 .  5 1
                  2 .  6 1
                  2 .  7 1
                  2 .  8 1
                  2 .  9 1
                  2 . 10 1
                  2 . 11 1
                  2 . 12 1
                  2 . 13 1
                  2 .  . 1
                  . 0  1 1
                  . 0  2 1
                  . 0  8 1
                  . 0  . 1
                  . 1  . 1
                  . .  1 1
                  . .  2 1
                  . .  3 1
                  . .  4 1
                  . .  5 1
                  . .  6 1
                  . .  7 1
                  . .  8 1
                  . . 12 1
                  . . 13 1
                  . .  . 1
                  end
                  label values Sex S
                  label def S 1 "M", modify
                  label def S 2 "F", modify
                  label values Jobs j
                  label def j 0 "no job", modify
                  label def j 1 "job", modify
                  label values School sch
                  label def sch 1 "1", modify
                  label def sch 2 "2", modify

                  Comment


                  • #10
                    Sorry, but that can't be your data. No cross-combination of categories occurs more than once. I don't know what you did wrong, but there's no useful graph from that.

                    Comment


                    • #11
                      Keira, does the solution in #3 not work?
                      Stata/MP 14.1 (64-bit x86-64)
                      Revision 19 May 2016
                      Win 8.1

                      Comment


                      • #12
                        Sorry, Nick, I might have launched twice the command contract. Is it now better?


                        Code:
                        * Example generated by -dataex-. To install: ssc install dataex
                        clear
                        input byte sex float jobs long(school _freq)
                        1 0  1  2579
                        1 0  2 31890
                        1 0  3    73
                        1 0  4  2109
                        1 0  5  6408
                        1 0  6    16
                        1 0  7  1163
                        1 0  8 34427
                        1 0  9  1392
                        1 0 10   847
                        1 0 11   669
                        1 0 12    85
                        1 0 13  7946
                        1 0  .  4480
                        1 1  1    33
                        1 1  2   104
                        1 1  4    10
                        1 1  5     4
                        1 1  8    27
                        1 1  9    56
                        1 1 10     8
                        1 1 11     4
                        1 1 12     3
                        1 1 13     2
                        1 1  .    31
                        2 0  1  1401
                        2 0  2 26951
                        2 0  3 10445
                        2 0  4  1830
                        2 0  5  3850
                        2 0  6  7459
                        2 0  7  1535
                        2 0  8 35771
                        2 0  9  1521
                        2 0 10   625
                        2 0 11   500
                        2 0 12    47
                        2 0 13  1563
                        2 0  .  4199
                        2 1  1    20
                        2 1  2    52
                        2 1  3     4
                        2 1  4     4
                        2 1  5     1
                        2 1  6     5
                        2 1  8    33
                        2 1  9    27
                        2 1 10     1
                        2 1 11     1
                        2 1 12     3
                        2 1 13     2
                        2 1  .    16
                        . 0  1     1
                        . 0  2     1
                        . 0  8     1
                        . 0  .     5
                        . 1  .     1
                        end
                        label values sex sex
                        label def sex 1 "M", modify
                        label def sex 2 "F", modify
                        label values jobs j
                        label def j 0 "no job", modify
                        label def j 1 "job", modify
                        label values school sch
                        label def sch 1 "1", modify
                        label def sch 2 "2", modify
                        label def sch 3 "3", modify
                        label def sch 4 "4", modify
                        label def sch 5 "5", modify
                        label def sch 6 "6", modify
                        label def sch 7 "7", modify
                        label def sch 8 "8", modify
                        label def sch 9 "9", modify
                        label def sch 10 "10", modify
                        label def sch 11 "11", modify
                        label def sch 12 "12", modify
                        label def sch 13 "13", modify

                        Comment


                        • #13
                          Carole, solution #3 gives the graph I showed in answer #5.... something still not working but I can't figure out what.

                          Comment


                          • #14
                            Good. Is this closer?

                            Code:
                            catplot sex school [fw=_freq], by(jobs, note("")) percent(jobs school) asyvars bar(1, bfcolor(blue*0.5))  bar(2, bfcolor(pink*0.5))

                            Click image for larger version

Name:	jobs.png
Views:	1
Size:	13.7 KB
ID:	1337777



                            Comment


                            • #15
                              Something like that, but not exactly. I need percentages on the total size of the sample, not on each category. Here it looks like that if I take M+F let's say, with job from school 8, then I have 100% (as from i.e. F at school 3, they already fill the 100%). So it looks like this graph allows me to see on a specific category, what is the percentage of M and of F.
                              I would like to classify the categories on the ENTIRE set of data, so that I could see on the entire population what is the percentage of let's say M, that have a job and comes from school 3.

                              Comment

                              Working...
                              X