Announcement

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

  • How to sort these bars with graph bar?

    Hi Statalisters,

    I would like the bars in the picture below to be sorted in descending order for each id and year. For example, the order should be blue, grey, green, red, and yellow for id 1 year 2005. I tried over(time, sort() descending)but without success.

    A simplified version of the code and a data example:

    Code:
    graph bar (sum) y1 y2 y3 y4 y5, over(time) over(id) stack
       
    id time y1 y2 y3 y4 y5
    1 2005q4 13.6 0.6 1.6 0.3 4.5
    1 2010q4 23.1 0.4 3.4 0.2 6.8
    1 2015q4 14.2 0.1 0.1 0.0 1.0
    2 2005q4 0.5 0.3 0.1 0.0 1.7
    2 2010q4 2.4 0.3 0.8 0.0 2.0
    2 2015q4 2.7 0.3 0.1 0.0 1.7


    Click image for larger version

Name:	Example.png
Views:	1
Size:	7.2 KB
ID:	1343929


    Cheers,
    Giovanni

  • #2
    There is no need of you having your data in wide form. Whatever you want to achieve, you can do it with your data in long form.

    Code:
    input id time y1 y2 y3 y4 y5
    1 2005 13.6 0.6 1.6 0.3 4.5
    1 2010 23.1 0.4 3.4 0.2 6.8
    1 2015 14.2 0.1 0.1 0.0 1.0
    2 2005 0.5 0.3 0.1 0.0 1.7
    2 2010 2.4 0.3 0.8 0.0 2.0
    2 2015 2.7 0.3 0.1 0.0 1.7
    end
    gen ID= _n
    reshape long y, i(ID) j(which)
    graph bar (sum) y, over(which, sort(y)) over(time)over(id)asyvars stack
    Click image for larger version

Name:	graph_A.PNG
Views:	1
Size:	10.5 KB
ID:	1343938

    Comment


    • #3
      Thanks Andrew for your suggestion. Unfortunately I think I can't use this approach as it would require me to restructure all my data and codes. Does anyone know another approach?

      Btw, I found out that Stata editor sorts the bars the way I want them to be sorted, although it changes the variable names to sum_of_y1, sum_of_y2, etc after the sorting. However, I still haven't figured out what the code is.
      Last edited by Giovanni Palmioli; 04 Jun 2016, 08:55.

      Comment


      • #4
        Just follow the same logic in my code. The general point is that on the most part, it is useful to work with data in long form. What do you mean it changes the variable names? That is exactly what they are... sums of each of your variables!

        Code:
        input id time y1 y2 y3 y4 y5
        1 2005 13.6 0.6 1.6 0.3 4.5
        1 2010 23.1 0.4 3.4 0.2 6.8
        1 2015 14.2 0.1 0.1 0.0 1.0
        2 2005 0.5 0.3 0.1 0.0 1.7
        2 2010 2.4 0.3 0.8 0.0 2.0
        2 2015 2.7 0.3 0.1 0.0 1.7
        end
        
        graph bar (sum) y*, over(time, sort(y*))over(id) stack
        If you are looking at how to relabel your variables, then that is a whole another issue. You can do this explicitly

        Code:
        graph bar (sum) y*, over(time, sort(y*))over(id) yvaroptions( relabel(1 "my label 1" 2 "my label 2" 3 "my label 3" 4 "my label 4" 5 "my label 5")) stack

        Attached Files
        Last edited by Andrew Musau; 04 Jun 2016, 10:27.

        Comment


        • #5
          Oh! I neglected to look at the final graph... I see the difficulty you face in simultaneously sorting all 5 variables (in fact observations because this time you are sorting horizontally). If you can do it in the editor and use the relabel command that I provide, then you are home and dry. Otherwise, you may need to transform your data to long form so that you can execute the sort.

          Of course, someone else may come up with a trick with data in wide form... this one beats me
          Last edited by Andrew Musau; 04 Jun 2016, 11:36.

          Comment

          Working...
          X