Announcement

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

  • Sorting the order of bar graph values while using by

    Hello!

    I want to order the results of a bar graph in descending order. I have two facets/panels as I want to disaggregate results by gender, male and female. But I want to order both panels/bars in descending order based on the order of how women responded. I am using this code:

    graph hbar Q101A , over( Q102 ) by( Q1002 )

    to get the attached graph but if I use the descending option the two panels are ordered differently. Any tips? Thanks!
    Click image for larger version

Name:	stata.png
Views:	1
Size:	59.1 KB
ID:	1595508

  • #2
    Code:
    gen order= -Q101A  
    graph hbar Q101A , over(Q102, sort(order)) by(Q1002)

    Comment


    • #3
      Thanks Andrew! That helped.

      Any one know how I can gen the order variable so that the plot appears in the order of the female plot for both?

      Comment


      • #4
        I do not completely get what you are after. My advice is to present a data example using dataex and then explain what you mean in #3.

        Comment


        • #5
          Thanks for the follow up Andrew.

          Here's the dataex export:
          CODE]
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input double(Q2061A Q1002)
          2 1
          8 1
          2 1
          2 1
          1 1
          14 1
          1 1
          1 1
          1 2
          1 2
          2 2
          14 2
          3 2
          2 2
          8 2
          1 2
          2 2
          1 2
          1 1
          12 2
          1 2
          3 2
          11 2
          11 2
          1 2
          14 2
          1 2
          12 2
          2 2
          2 2
          1 2
          1 2
          2 1
          6 1
          12 1
          1 1
          1 1
          12 1
          1 1
          6 2
          1 2
          2 2
          1 2
          12 2
          1 2
          7 1
          7 1
          12 1
          7 1
          2 1
          12 1
          1 1
          12 2
          12 2
          2 2
          1 2
          1 2
          1 2
          2 1
          12 1
          1 1
          1 1
          1 1
          11 1
          1 1
          2 2
          2 2
          3 2
          1 2
          11 2
          12 2
          1 2
          3 1
          1 1
          1 1
          2 1
          1 1
          1 1
          1 1
          2 2
          1 2
          12 2
          2 2
          2 2
          1 2
          1 2
          7 1
          6 1
          14 1
          1 1
          1 1
          1 1
          2 1
          1 1
          2 1
          3 1
          1 1
          7 1
          7 1
          8 1
          end
          label values Q2061A Q2061A
          label def Q2061A 1 "economic situation", modify
          label def Q2061A 2 "financial and administrative corruption", modify
          label def Q2061A 3 "democracy and representation/governance", modify
          label def Q2061A 6 "internal stability and security", modify
          label def Q2061A 7 "foreign interference", modify
          label def Q2061A 8 "religious extremism", modify
          label def Q2061A 11 "fighting terrorism", modify
          label def Q2061A 12 "public services", modify
          label def Q2061A 14 "political/party issues", modify
          label values Q1002 Q1002
          label def Q1002 1 "male", modify
          label def Q1002 2 "female", modify
          [/CODE]
          copy up to and including the previous line ------ -----------

          I use this code: graph hbar, over(Q2061A) by(Q1002) graph attached for reference

          What I want to add here is a way to sort both panels by the descending order of female respondents if that makes sense?
          Click image for larger version

Name:	stata2.png
Views:	1
Size:	58.8 KB
ID:	1595638

          Comment


          • #6
            I saw some mention of the egenmore axis and the seqvar commands but did not really comprehend how they could assist.

            Comment


            • #7
              I think this will do what you want. As you are grouping categories, there is some redundancy in having labels for each by-graph. Look at tabplot from the Stata Journal that has a more appealing design and search the forum for code examples.

              Code:
              preserve
              contract Q2061A Q1002
              fillin Q2061A Q1002
              replace _freq=0 if missing(_freq)
              gen order=-_freq if Q1002==2
              bys Q2061A: replace order= order[_N]
              graph hbar _freq, over(Q2061A, sort(order)) by(Q1002)
              restore

              Comment


              • #8
                This is exactly what I was looking for, thank you a lot. Any idea how to use aweights with this?

                Comment


                • #9
                  This picks up on Andrew Musau's suggestion in #7. Ordering on the frequencies (equivalently percents) within a subgroup is programmable.

                  You have to find subgroup frequencies, inform the other group(s) what they are, and sort and make sure to copy back category names.

                  Some of this code is very similar to Andrew's, although he is missing the zero option of contract (perhaps misnamed).

                  labmask and tabplot are from the Stata Journal.

                  tabplot does often require some tweaking of where text goes and how much space you give to it. 0.23 and 0.85 here are the result of trial and error.


                  Code:
                  bysort Q2061A Q1002: gen freq = _N if Q1002 == 2
                  by Q2061A (Q1002) : replace freq = -freq[_N]
                  
                  egen group = group(freq Q2061A)
                  labmask group, values(Q2061A) decode  
                  
                  tabplot group Q1002 , percent(Q1002) showval(offset(0.23)) xsc(r(0.85 .)) yla(, labsize(small)) ytitle("") xtitle("") horizontal subtitle(% by gender) separate(Q1002) bar1(color(blue)) bar2(color(orange_red))
                  Click image for larger version

Name:	cea.png
Views:	1
Size:	34.4 KB
ID:	1595706



                  tabplot supports analytic weights. How that bears on your example I do not know.
                  Last edited by Nick Cox; 01 Mar 2021, 10:24.

                  Comment


                  • #10
                    Originally posted by Nick Cox View Post
                    This picks up on Andrew Musau's suggestion in #7. Ordering on the frequencies (equivalently percents) within a subgroup is programmable.

                    You have to find subgroup frequencies, inform the other group(s) what they are, and sort and make sure to copy back category names.

                    Some of this code is very similar to Andrew's, although he is missing the zero option of contract (perhaps misnamed).

                    labmask and tabplot are from the Stata Journal.

                    tabplot does often require some tweaking of where text goes and how much space you give to it. 0.23 and 0.85 here are the result of trial and error.


                    Code:
                    bysort Q2061A Q1002: gen freq = _N if Q1002 == 2
                    by Q2061A (Q1002) : replace freq = -freq[_N]
                    
                    egen group = group(freq Q2061A)
                    labmask group, values(Q2061A) decode
                    
                    tabplot group Q1002 , percent(Q1002) showval(offset(0.23)) xsc(r(0.85 .)) yla(, labsize(small)) ytitle("") xtitle("") horizontal subtitle(% by gender) separate(Q1002) bar1(color(blue)) bar2(color(orange_red))
                    [ATTACH=CONFIG]n1595706[/ATTACH]


                    tabplot supports analytic weights. How that bears on your example I do not know.
                    Thanks so much Nick!

                    This is really helpful. For some reason this doesn't seem to work with more than two subgroups, so for example if I replace gender with age:

                    This function: "by Q2061A (Q1002) : replace freq = -freq[_N]"

                    replaces everything with missing, any tips?

                    Thanks again

                    Comment


                    • #11
                      In your full dataset you may have values of Q2061A for which it is never true that Q1002 is 2.

                      Code:
                      tab Q2061A Q1002
                      will tell you that.

                      Comment


                      • #12
                        Originally posted by Nick Cox View Post
                        In your full dataset you may have values of Q2061A for which it is never true that Q1002 is 2.

                        Code:
                        tab Q2061A Q1002
                        will tell you that.
                        Sorry for bothering you again with this but here's the dataex of the two variables I am now trying to plot:


                        ----------------------- copy starting from the next line -----------------------
                        Code:
                        * Example generated by -dataex-. To install: ssc install dataex
                        clear
                        input double(Q609 Q2061A)
                        2  2
                        2  8
                        2  2
                        2  2
                        2  1
                        1 14
                        1  1
                        2  1
                        3  1
                        2  1
                        1  2
                        1 14
                        2  3
                        3  2
                        1  8
                        1  1
                        2  2
                        3  1
                        3  1
                        1 12
                        2  1
                        2  3
                        2 11
                        2 11
                        2  1
                        3 14
                        3  1
                        1 12
                        2  2
                        1  2
                        3  1
                        2  1
                        2  2
                        1  6
                        1 12
                        1  1
                        3  1
                        1 12
                        2  1
                        2  6
                        2  1
                        2  2
                        2  1
                        1 12
                        2  1
                        2  7
                        2  7
                        1 12
                        2  7
                        3  2
                        1 12
                        2  1
                        2 12
                        1 12
                        2  2
                        1  1
                        1  1
                        2  1
                        2  2
                        2 12
                        1  1
                        1  1
                        2  1
                        2 11
                        2  1
                        1  2
                        2  2
                        2  3
                        2  1
                        1 11
                        1 12
                        2  1
                        3  3
                        3  1
                        2  1
                        2  2
                        3  1
                        2  1
                        2  1
                        2  2
                        3  1
                        1 12
                        2  2
                        1  2
                        1  1
                        2  1
                        3  7
                        2  6
                        2 14
                        2  1
                        1  1
                        3  1
                        1  2
                        2  1
                        1  2
                        2  3
                        2  1
                        2  7
                        2  7
                        1  8
                        end
                        label values Q609 Q609
                        label def Q609 1 "religious", modify
                        label def Q609 2 "somewhat religious", modify
                        label def Q609 3 "not religious", modify
                        label values Q2061A Q2061A
                        label def Q2061A 1 "economic situation", modify
                        label def Q2061A 2 "financial and administrative corruption", modify
                        label def Q2061A 3 "democracy and representation/governance", modify
                        label def Q2061A 6 "internal stability and security", modify
                        label def Q2061A 7 "foreign interference", modify
                        label def Q2061A 8 "religious extremism", modify
                        label def Q2061A 11 "fighting terrorism", modify
                        label def Q2061A 12 "public services", modify
                        label def Q2061A 14 "political/party issues", modify
                        ------------------ copy up to and including the previous line ------------------


                        And here's the code I'm using to group:

                        bysort Q2061A Q609: gen freq = _N if Q609 == 1
                        by Q2061A (Q609) : replace freq = -freq[_N]

                        egen group = group(freq Q2061A)
                        labmask group, values(Q2061A) decode

                        Any help would be appreciated!

                        Also is there a way for the tabplot subcommand showval to place the values to the right not left?

                        Comment


                        • #13
                          To help further, I need to see the results of

                          Code:
                           
                           tab Q2061A Q1002 
                          tabplot, showval That is not supported. You'd need to write your own program.

                          Comment


                          • #14
                            Originally posted by Nick Cox View Post
                            To help further, I need to see the results of

                            Code:
                            tab Q2061A Q1002 
                            tabplot, showval That is not supported. You'd need to write your own program.
                            of course! thanks in advance!
                            MOST IMPORTANT LEVEL OF RELIGIOSITY
                            CHALLENGE religious somewhat not relig don't kno refused Total
                            economic situation 3,327 4,949 1,144 124 41 9,585
                            financial and adminis 1,310 2,131 444 47 22 3,954
                            democracy and represe 172 275 78 7 2 534
                            internal stability an 417 649 154 12 1 1,233
                            foreign interference 721 1,128 206 18 8 2,081
                            religious extremism 280 357 99 11 5 752
                            fighting terrorism 666 705 254 18 4 1,647
                            public services 911 1,197 255 24 5 2,392
                            security 446 567 123 16 1 1,153
                            political/party issue 317 458 162 13 6 956
                            other 261 385 118 40 8 812
                            don't know 103 96 39 14 0 252
                            refused 22 9 5 0 0 36
                            Total 8,953 12,906 3,081 344 103 25,387

                            Comment


                            • #15
                              These are different variables. But zeros in the table would be problematic. You have a choice. You can either pursue #10 in which case #11 and #13 still apply; or pursue #14 in which case we need to see (1) the graph code you tried and (2) the results of


                              Code:
                              contract , zero 
                              dataex
                              with the two variables in #14. So if the variables were a b (they aren't) you need to go

                              Code:
                              contract a b, zero 
                              dataex
                              See also https://www.statalist.org/forums/help#stata for how to present data examples here.

                              Comment

                              Working...
                              X