Announcement

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

  • how to graph stacked bar with each category sorted in order of percentage

    Hi Statalisters,
    I'm trying to graph a stacked bar graph with each of the categories sorted in order of highest percentage to the smallest. Does anyone know how best to deal with this? Example data here

    Code:
    clear        
    input byte cat    hdl_1    hdl_2    hdl_3    hdl_4    hdl_5    hdl_6    hdl_7    hdl_8    hdl_9    hdl_10    hdl_11    hdl_12    hdl_13    hdl_14    hdl_15    hdl_16    
    1    1.524952    19.75993    18.82018    6.359863    4.816993    0    15.50487    3.358603    17.13577    2.528465    1.368276    1.073384    2.954223    0    .2096547    4.452342    
    2    .9871029    16.283    20.67916    5.465534    5.295229    0    13.42496    2.838352    25.91014    2.068441    1.36044    .5245776    1.783914    .0988639    0    3.128761    
    3    1.203603    17.68257    19.93086    5.825528    5.102724    0    14.26219    3.047768    22.37819    2.253614    1.363594    .7454886    2.254999    .0590682    .0843923    3.661542
    end    
    label values cat catl
    label def catl 1 "Monday", modify
    label def catl 2 "Wednesday", modify
    label def catl 3 "Friday", modify
    The -sort- option command isn't working for me following the below code
    Code:
    gr bar hdl_1- hdl_16, over(cat, sort(#)) stack percent blabel(bar, position(center) format(%4.0f))
    Thanks

  • #2
    # is generic syntax; you need to type 1 or 2 or whatever other number you want.


    -- except that I doubt that you can improve the graph that way.

    I don't understand the data beyond Mon Wed Fri -- unless HDL means high-density lipoprotein -- but a stacked bar chart is doomed to disappoint here. The legend alone will in this case take up about half the available space, and the result will be somewhere between difficult and impossible to work with.

    I played a bit using tabplot and myaxis from the Stata Journal.

    Code:
    clear        
    input byte cat    hdl_1    hdl_2    hdl_3    hdl_4    hdl_5    hdl_6    hdl_7    hdl_8    hdl_9    hdl_10    hdl_11    hdl_12    hdl_13    hdl_14    hdl_15    hdl_16    
    1    1.524952    19.75993    18.82018    6.359863    4.816993    0    15.50487    3.358603    17.13577    2.528465    1.368276    1.073384    2.954223    0    .2096547    4.452342    
    2    .9871029    16.283    20.67916    5.465534    5.295229    0    13.42496    2.838352    25.91014    2.068441    1.36044    .5245776    1.783914    .0988639    0    3.128761    
    3    1.203603    17.68257    19.93086    5.825528    5.102724    0    14.26219    3.047768    22.37819    2.253614    1.363594    .7454886    2.254999    .0590682    .0843923    3.661542
    end    
    label values cat catl
    label def catl 1 "Monday", modify
    label def catl 2 "Wednesday", modify
    label def catl 3 "Friday", modify
    
    set scheme s1color 
    
    gr bar (asis) hdl_1-hdl_16, over(cat) stack percent blabel(bar, position(center) format(%4.0f)) name(G1, replace)
    
    reshape long hdl_, i(cat) j(which)
    
    
    tabplot which cat [iw=hdl_] , horiz showval(format(%2.1f) offset(0.45)) xsc(r(0.85 .)) subtitle(HDL) name(G2, replace)
    
    myaxis yaxis=which, sort(mean hdl_) descending 
    
    tabplot yaxis cat [iw=hdl_] , horiz showval(format(%2.1f) offset(0.45)) xsc(r(0.85 .)) subtitle(HDL) xtitle("") name(G3, replace)
    Click image for larger version

Name:	HDL1.png
Views:	1
Size:	25.0 KB
ID:	1709495
    Click image for larger version

Name:	HDL2.png
Views:	1
Size:	23.9 KB
ID:	1709496
    Click image for larger version

Name:	HDL3.png
Views:	1
Size:	23.5 KB
ID:	1709497


    Last edited by Nick Cox; 12 Apr 2023, 06:19.

    Comment


    • #3
      Thanks Nick, even when I provide a number, it still doesn't provide the desired result. It just switches the order of the category, but not the order of percentages per category. The output I'm looking for is attached.

      I have omitted the legend in this snip of the output I'm after. But as can see, the percentages are sorted in descending order for each gender.
      Attached Files
      Last edited by Madu Abuchi; 12 Apr 2023, 06:36.

      Comment


      • #4
        I see what you want, but I can’t help further. I don’t know how to get that in Stata and in any case the graph is meaningless without legend and would be useless with one. Sorry to disappoint.

        Comment


        • #5
          No worries, thanks Nick for your time

          Comment


          • #6
            As a qualifier to #4 I now think what you want is programmable using twoway rbar, but not trivially. As in your example you need a legend with 16 entries I still can't rustle up any enthusiasm to code up what strikes me as a flawed design.

            Comment


            • #7
              Thanks Nick, I will read up how that can be done using twoway rbar and attempt. What I really need is to have each category like in #3 sorted in descending order of the percentages of the variables that forms the legend.
              The standard stacked bar graph fix things in the order the variables are positioned in the code with little or no flexibility.

              Comment

              Working...
              X