Announcement

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

  • graph bar: order the sequence of bars by its value (height)

    Hi, here is my code in a simple way:

    graph bar a b c d, over(X)

    X can be thought as countries, and a b c d as number of employees in each country hired by 4 global companies. Now within each country, how can I plot the bars representing number of employees so that they are ranked by their value (height)? By default, they are plotted as they are in listed in the code (a b c and d). However, I really wish to change this order according to the value of a b c and d so that its easy to see which company hires the most in any specific country. For example, in country A, a=1 b=2 c=3 d=4 so bars in a descending order should be d c b and a. In country B, a=1 b=3 c=2 d=4, then the order should be d b c and a.

    Thank you.

    Mike

  • #2
    Unfortunately, I cannot show and example now.

    But you wish to use the option - sort(#)- for that.
    Best regards,

    Marcos

    Comment


    • #3
      Originally posted by Marcos Almeida View Post
      Unfortunately, I cannot show and example now.

      But you wish to use the option - sort(#)- for that.
      Thank you Marcos for you quick reply. My impression is that sort() change the order of the countries not the bars (a b c d) within each country.

      Comment


      • #4
        So, @Mike, as your analysis implies, you need a different data structure that solves that.

        A data example, as requested in FAQ Advice #12, would have yielded some faster answers, or so I guess.

        Meanwhile, running this code on this silly example will show some technique:

        Code:
        clear
        set obs 3
        set seed 2803 
        gen country = word("Alpha Beta Gamma", _n) 
        
        foreach v in A B C D { 
           gen `v' = runiform()
        } 
        
        list 
        
        graph bar (asis) A B C D, over(country) 
        
        rename (A B C D) (y=) 
        
        reshape long y, i(country) j(which) string 
        
        list , sepby(country) 
        
        graph bar (asis) y, over(which, sort(1) descending) over(country) 
        
        graph bar (asis) y, over(which, sort(1) descending) over(country) asyvars





        Comment


        • #5
          Originally posted by Nick Cox View Post
          So, @Mike, as your analysis implies, you need a different data structure that solves that.

          A data example, as requested in FAQ Advice #12, would have yielded some faster answers, or so I guess.

          Meanwhile, running this code on this silly example will show some technique:

          Code:
          clear
          set obs 3
          set seed 2803
          gen country = word("Alpha Beta Gamma", _n)
          
          foreach v in A B C D {
          gen `v' = runiform()
          }
          
          list
          
          graph bar (asis) A B C D, over(country)
          
          rename (A B C D) (y=)
          
          reshape long y, i(country) j(which) string
          
          list , sepby(country)
          
          graph bar (asis) y, over(which, sort(1) descending) over(country)
          
          graph bar (asis) y, over(which, sort(1) descending) over(country) asyvars




          Thanks a lot Nick!

          I initially used the same data structure as you suggested but I never paid attention to the "asyvars" option. I thought the only way these bars would show up in different colors was to use multiple variables. Obviously stata is smarter than that. Thanks!

          Mike

          Comment

          Working...
          X