Announcement

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

  • Stacking a bar graph

    Hi,

    I have a binary variable (childcare_nofsa) and a categorical variable (state). I'd like to produce a stacked bar graph showing the frequencies of "provided" and "not provided" response categories in childcare_nofsa per state.

    Here are data examples for the two variables:

    childcare_nofsa

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float childcare_nofsa
    0
    1
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    1
    0
    0
    0
    1
    0
    1
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    1
    0
    1
    0
    0
    0
    0
    0
    0
    1
    0
    0
    0
    0
    0
    0
    0
    0
    1
    0
    0
    0
    0
    0
    .
    0
    0
    0
    0
    0
    0
    0
    0
    .
    0
    0
    0
    0
    0
    0
    0
    1
    0
    0
    0
    0
    0
    0
    0
    0
    0
    1
    0
    1
    0
    0
    0
    0
    0
    1
    0
    end
    label values childcare_nofsa childcare_nofsa
    label def childcare_nofsa 0 "Not provided", modify
    label def childcare_nofsa 1 "Provided", modify

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float state
    1
    1
    1
    1
    1
    2
    2
    2
    2
    2
    2
    2
    3
    3
    3
    3
    3
    3
    4
    4
    4
    4
    4
    4
    4
    4
    4
    4
    4
    4
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    5
    end
    label values state state
    label def state 1 "AK", modify
    label def state 2 "AL", modify
    label def state 3 "AR", modify
    label def state 4 "AZ", modify
    label def state 5 "CA", modify
    So far, I've used the following code

    Code:
    graph bar (count) childcare_nofsa, over(state) stack
    That gets me the digital picture I've inserted here.

    Click image for larger version

Name:	Screenshot 2021-11-24 at 12.55.26.png
Views:	1
Size:	40.4 KB
ID:	1637917


    What I'd like to achieve however is
    (1) Stacking the responses in terms of the relative frequencies of the values of 1 and 0 in "childcare_nofsa"
    (2) Getting the states names to appear vertically so they are all individually legible.
    --> I've added an analog picture of what's in my mind's eye.

    Click image for larger version

Name:	Screenshot 2021-11-24 at 13.00.06.png
Views:	1
Size:	284.5 KB
ID:	1637918


    Grateful for any help

  • #2
    Please give a data example including both your variables.

    You may need to re-think your design any way. Minimally, you have 50 states (and what about DC, Puerto Rico, Guam, and so forth?). Will your design really be readable even with vertical labels? What about a horizontal chart?

    Isn't it predictable that CA NY TX and so on will just dwarf WY and so on?

    Comment


    • #3
      Thanks for your response Nick.

      I gave data examples of each variable, but did you mean together?

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input float(childcare_nofsa state)
      0 1
      0 1
      0 1
      0 1
      1 1
      0 2
      0 2
      0 2
      0 2
      0 2
      0 2
      0 2
      0 3
      0 3
      1 3
      0 3
      0 3
      0 3
      0 4
      0 4
      1 4
      0 4
      0 4
      0 4
      1 4
      0 4
      0 4
      0 4
      0 4
      0 4
      0 5
      1 5
      0 5
      0 5
      1 5
      0 5
      0 5
      0 5
      0 5
      0 5
      0 5
      0 5
      0 5
      1 5
      0 5
      0 5
      0 5
      1 5
      1 5
      0 5
      0 5
      0 5
      0 5
      0 5
      0 5
      0 5
      0 5
      0 5
      0 5
      0 5
      0 5
      0 5
      0 5
      . 5
      0 5
      0 5
      0 5
      0 5
      0 5
      . 5
      0 5
      0 5
      0 5
      0 5
      0 5
      0 5
      0 5
      0 5
      0 5
      0 5
      1 5
      0 5
      0 5
      0 5
      0 5
      1 5
      0 5
      0 5
      0 5
      0 5
      0 5
      0 5
      0 5
      0 5
      0 5
      0 5
      0 5
      0 5
      0 5
      0 5
      end
      label values childcare_nofsa childcare_nofsa
      label def childcare_nofsa 0 "Not provided", modify
      label def childcare_nofsa 1 "Provided", modify
      label values state state
      label def state 1 "AK", modify
      label def state 2 "AL", modify
      label def state 3 "AR", modify
      label def state 4 "AZ", modify
      label def state 5 "CA", modify
      Regarding chart design: Indeed, I have the 50 states + DC. And exactly those three states do dwarf the others. But I think I need the graph to make a larger point about my analysis. I also may end up only constructing a graph with a smaller selection of states, but for that reason too it would be great to know how.

      Comment


      • #4
        Yes, thanks; that is the kind of data example I was seeking.

        graph bar and graph hbar can accommodate what you're asking for, at least since Stata 13 as updated on 9 October 2014, but I remain more familiar with the convenience wrapper catplot from SSC I first wrote in 2003 and revised in 2010. .

        Here is a stab at a horizontal bar chart for your example.


        Code:
        bysort state : gen total = _N 
        
        set scheme s1color 
        
        * ssc install catplot 
        catplot childcare_nofsa state, asyvars stack var2opts(sort(total) descending) bar(1, bfcolor(red*0.2) blcolor(red)) bar(2, bfcolor(blue*0.2) blcolor(blue))
        Click image for larger version

Name:	childcare.png
Views:	1
Size:	18.5 KB
ID:	1638106

        Comment


        • #5
          Thanks a lot Nick! This is what I've got now. Do you know how I could space the bars so the labels of the states don't overlap?

          Click image for larger version

Name:	Screenshot 2021-11-25 at 17.35.40.png
Views:	1
Size:	47.5 KB
ID:	1638125

          Comment


          • #6
            This is precisely the problem warned about in #2. So you can make your text smaller, change the aspect ratio, make your graph larger or recast in two panels as explained in section 6 of https://www.stata-journal.com/articl...article=gr0080

            To get more specific help, please show the results of

            Code:
            contract state childcare_nofsa
            dataex

            Comment


            • #7
              Ah, when you asked "will the design really be readable" I thought you meant having to read states names vertically. I didn't realise they would be illegible because they might physically overlap. Anyway, here is the data you requested:

              Code:
              * Example generated by -dataex-. To install: ssc install dataex
              clear
              input float(childcare_nofsa state) byte _freq
              0  1  4
              1  1  1
              0  2  7
              0  3  5
              1  3  1
              0  4 10
              1  4  2
              0  5 67
              1  5  8
              .  5  2
              0  6 19
              1  6  3
              0  7  9
              1  7  1
              0  8  2
              1  8  1
              0  9  5
              0 10 26
              1 10  8
              0 11 15
              1 11  6
              0 12  2
              0 13 20
              1 13  1
              0 14  5
              0 15 42
              1 15  9
              0 16 17
              1 16  2
              0 17 15
              1 17  2
              0 18  8
              0 19  8
              1 19  2
              0 20 23
              1 20  2
              0 21 14
              1 21  2
              0 22 10
              0 23 28
              1 23  3
              0 24 33
              1 24  2
              0 25  9
              1 25  9
              0 26  4
              1 26  1
              0 27  3
              1 27  2
              0 28 11
              1 28  2
              0 29  3
              0 30  4
              1 30  2
              0 31  7
              1 31  1
              0 32 22
              1 32  5
              . 32  1
              0 33  3
              1 33  1
              0 34  5
              0 35 32
              1 35  7
              0 36 30
              1 36  6
              0 37  7
              0 38 13
              1 38  3
              0 39 35
              1 39  5
              . 39  2
              0 40  1
              1 40  1
              0 41  8
              1 41  3
              0 42  6
              1 42  1
              0 43 13
              1 43  2
              . 43  1
              0 44 57
              1 44  5
              0 45  7
              1 45  1
              0 46 30
              1 46  5
              . 46  1
              0 47  2
              1 47  1
              0 48 18
              1 48  3
              0 49 30
              1 49  4
              0 50  3
              1 50  1
              1 51  2
              0  . 16
              1  .  8
              .  .  2
              end
              label values childcare_nofsa childcare_nofsa
              label def childcare_nofsa 0 "Not provided", modify
              label def childcare_nofsa 1 "Provided", modify
              label values state state
              label def state 1 "AK", modify
              label def state 2 "AL", modify
              label def state 3 "AR", modify
              label def state 4 "AZ", modify
              label def state 5 "CA", modify
              label def state 6 "CO", modify
              label def state 7 "CT", modify
              label def state 8 "DC", modify
              label def state 9 "DE", modify
              label def state 10 "FL", modify
              label def state 11 "GA", modify
              label def state 12 "HI", modify
              label def state 13 "IA", modify
              label def state 14 "ID", modify
              label def state 15 "IL", modify
              label def state 16 "IN", modify
              label def state 17 "KS", modify
              label def state 18 "KY", modify
              label def state 19 "LA", modify
              label def state 20 "MA", modify
              label def state 21 "MD", modify
              label def state 22 "ME", modify
              label def state 23 "MI", modify
              label def state 24 "MN", modify
              label def state 25 "MO", modify
              label def state 26 "MS", modify
              label def state 27 "MT", modify
              label def state 28 "NC", modify
              label def state 29 "ND", modify
              label def state 30 "NE", modify
              label def state 31 "NH", modify
              label def state 32 "NJ", modify
              label def state 33 "NM", modify
              label def state 34 "NV", modify
              label def state 35 "NY", modify
              label def state 36 "OH", modify
              label def state 37 "OK", modify
              label def state 38 "OR", modify
              label def state 39 "PA", modify
              label def state 40 "RI", modify
              label def state 41 "SC", modify
              label def state 42 "SD", modify
              label def state 43 "TN", modify
              label def state 44 "TX", modify
              label def state 45 "UT", modify
              label def state 46 "VA", modify
              label def state 47 "VT", modify
              label def state 48 "WA", modify
              label def state 49 "WI", modify
              label def state 50 "WV", modify
              label def state 51 "WY", modify

              Thanks again for taking the time to walk be through this.

              Comment


              • #8
                Here's some technique with your data example repeated for convenience.

                In terms of

                you can make your text smaller, change the aspect ratio, make your graph larger or recast in two panels as explained in section 6 of https://www.stata-journal.com/articl...article=gr0080
                this uses only the last trick, so

                make your text smaller, change the aspect ratio, make your graph larger
                remain as possibilities.


                Code:
                * Example generated by -dataex-. To install: ssc install dataex
                clear
                input float(childcare_nofsa state) byte _freq
                0  1  4
                1  1  1
                0  2  7
                0  3  5
                1  3  1
                0  4 10
                1  4  2
                0  5 67
                1  5  8
                .  5  2
                0  6 19
                1  6  3
                0  7  9
                1  7  1
                0  8  2
                1  8  1
                0  9  5
                0 10 26
                1 10  8
                0 11 15
                1 11  6
                0 12  2
                0 13 20
                1 13  1
                0 14  5
                0 15 42
                1 15  9
                0 16 17
                1 16  2
                0 17 15
                1 17  2
                0 18  8
                0 19  8
                1 19  2
                0 20 23
                1 20  2
                0 21 14
                1 21  2
                0 22 10
                0 23 28
                1 23  3
                0 24 33
                1 24  2
                0 25  9
                1 25  9
                0 26  4
                1 26  1
                0 27  3
                1 27  2
                0 28 11
                1 28  2
                0 29  3
                0 30  4
                1 30  2
                0 31  7
                1 31  1
                0 32 22
                1 32  5
                . 32  1
                0 33  3
                1 33  1
                0 34  5
                0 35 32
                1 35  7
                0 36 30
                1 36  6
                0 37  7
                0 38 13
                1 38  3
                0 39 35
                1 39  5
                . 39  2
                0 40  1
                1 40  1
                0 41  8
                1 41  3
                0 42  6
                1 42  1
                0 43 13
                1 43  2
                . 43  1
                0 44 57
                1 44  5
                0 45  7
                1 45  1
                0 46 30
                1 46  5
                . 46  1
                0 47  2
                1 47  1
                0 48 18
                1 48  3
                0 49 30
                1 49  4
                0 50  3
                1 50  1
                1 51  2
                0  . 16
                1  .  8
                .  .  2
                end
                label values childcare_nofsa childcare_nofsa
                label def childcare_nofsa 0 "Not provided", modify
                label def childcare_nofsa 1 "Provided", modify
                label values state state
                label def state 1 "AK", modify
                label def state 2 "AL", modify
                label def state 3 "AR", modify
                label def state 4 "AZ", modify
                label def state 5 "CA", modify
                label def state 6 "CO", modify
                label def state 7 "CT", modify
                label def state 8 "DC", modify
                label def state 9 "DE", modify
                label def state 10 "FL", modify
                label def state 11 "GA", modify
                label def state 12 "HI", modify
                label def state 13 "IA", modify
                label def state 14 "ID", modify
                label def state 15 "IL", modify
                label def state 16 "IN", modify
                label def state 17 "KS", modify
                label def state 18 "KY", modify
                label def state 19 "LA", modify
                label def state 20 "MA", modify
                label def state 21 "MD", modify
                label def state 22 "ME", modify
                label def state 23 "MI", modify
                label def state 24 "MN", modify
                label def state 25 "MO", modify
                label def state 26 "MS", modify
                label def state 27 "MT", modify
                label def state 28 "NC", modify
                label def state 29 "ND", modify
                label def state 30 "NE", modify
                label def state 31 "NH", modify
                label def state 32 "NJ", modify
                label def state 33 "NM", modify
                label def state 34 "NV", modify
                label def state 35 "NY", modify
                label def state 36 "OH", modify
                label def state 37 "OK", modify
                label def state 38 "OR", modify
                label def state 39 "PA", modify
                label def state 40 "RI", modify
                label def state 41 "SC", modify
                label def state 42 "SD", modify
                label def state 43 "TN", modify
                label def state 44 "TX", modify
                label def state 45 "UT", modify
                label def state 46 "VA", modify
                label def state 47 "VT", modify
                label def state 48 "WA", modify
                label def state 49 "WI", modify
                label def state 50 "WV", modify
                label def state 51 "WY", modify
                
                set scheme s1color  
                
                gen which = state > 26 
                rename _freq freq 
                egen total = total(-freq), by(state)
                
                local colors bar(1, fcolor(red*0.2) lcolor(red)) bar(2, fcolor(blue*0.2) lcolor(blue))
                
                
                graph hbar (count) [fw=freq] , over(childcare_nofsa) over(state) by(which, note("")) nofill stack asyvars subtitle("", fcolor(none)) name(G1, replace) ///
                `colors'
                
                egen group = group(total state) 
                labmask group, values(state) decode 
                replace which = group > 26 
                
                graph hbar (count) [fw=freq] , over(childcare_nofsa) over(group) by(which, note("")) nofill stack asyvars subtitle("", fcolor(none)) name(G2, replace) ///
                `colors'

                Click image for larger version

Name:	blau1.png
Views:	1
Size:	33.4 KB
ID:	1638618



                Click image for larger version

Name:	blau2.png
Views:	1
Size:	33.5 KB
ID:	1638619

                Comment


                • #9
                  Thanks a lot Nick! This is very helpful and I think I can tweak it from here.

                  Comment

                  Working...
                  X