Announcement

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

  • Yet another topic on ordering in bar graphs

    I know these types of question seem silly but I have not found a solution with digging through the archives. I want to plot the values of variable 'yhat' over id and type, but I want to give the bars a colour using the asyvars option. In addition, I want to sort the order of the bars according to yhat. I have tried the following:
    Code:
    graph bar yhat, over(type) over(id, sort(1)) asyvars nofill
    What happens, is my bars are no longer sort. They are sorted if I use
    Code:
    graph bar yhat, over(id, sort(1))  nofill
    A data example:
    Code:
         +--------------------------------+
         | id              type      yhat |
         |--------------------------------|
      1. |       40          A   4.432507 |
      2. |       48          A    5.10135 |
      3. |       48          A   5.611093 |
      4. |       48          A   6.062966 |
      5. |       71          C   6.086926 |
         |--------------------------------|
      6. |       79          A   6.172667 |
      7. |       48          A   6.176242 |
      8. |       48          A    6.28208 |
      9. |       26          A   6.564067 |
     10. |       79          A   6.851093 |
         |--------------------------------|
     11. |       26          A   7.008502 |
     12. |       32          C   7.029361 |
     13. |       71          C    7.10641 |
     14. |       26          A   7.187085 |
     15. |       29          B   7.245084 |
         |--------------------------------|
     16. |       35          A   7.364397 |
     17. |       64          A   7.626135 |
     18. |       32          C   7.707787 |
     19. |       32          C   7.709017 |
     20. |       26          A   7.753465 |
         +--------------------------------+

  • #2
    Next time use dataex to present data examples. Chances are you did not get a faster reply because it is just too much work to transform the data that you present above.

    . I want to plot the values of variable 'yhat' over id and type, but I want to give the bars a colour using the asyvars option. In addition, I want to sort the order of the bars according to yhat.
    Once you specify the option asyvars, Stata recognizes that each "y var" category is unique. Therefore, since your sort order depends on the variable "yhat" and not "id", generate a variable that defines the order first.

    Code:
    input id str5 type yhat 
    40 "A" 4.432507 
    48 "A" 5.10135 
    48 "A" 5.611093 
    48 "A" 6.062966 
    71 "C" 6.086926 
    79 "A" 6.172667 
    48 "A" 6.176242 
    48 "A" 6.28208 
    26 "A" 6.564067 
    79 "A" 6.851093
    26 "A" 7.008502 
    32 "C" 7.029361 
    71 "C" 7.10641 
    26 "A" 7.187085 
    29 "B" 7.245084 
    35 "A" 7.364397 
    64 "A" 7.626135 
    32 "C" 7.707787 
    32 "C" 7.709017 
    26 "A" 7.753465 
    end
    
    sort yhat
    gen order=_n
    graph bar yhat, over(type) over(id, sort(order)) asyvars nofill

    Comment

    Working...
    X