Announcement

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

  • Graph bar for categorical variable, but sorted in prespecified order

    This is a stunningly simple problem and I know there is a stunningly simple answer, but I can't figure it out.

    I want to graph a categorical variable, such as a grade distribution of letter grades. I can do this pretty easily with:

    Code:
    graph bar, over(grade)
    The problem with this is that it's out of order, with A coming before A+, B coming before B+. In addition, I would prefer to reverse it so that "F" is at the bottom.

    Based on the help file, I think the right solution is to create a new variable and then sort by it. So I did this:

    Code:
    gen ordvar=1 if Grade=="F"
    replace ordvar=2 if Grade=="D-"
    replace ordvar=3 if Grade=="D"
    replace ordvar=4 if Grade=="D+"
    replace ordvar=5 if Grade=="C-"
    replace ordvar=6 if Grade=="C"
    replace ordvar=7 if Grade=="C+"
    replace ordvar=8 if Grade=="B-"
    replace ordvar=9 if Grade=="B"
    replace ordvar=10 if Grade=="B+"
    replace ordvar=11 if Grade=="A-"
    replace ordvar=12 if Grade=="A"
    replace ordvar=13 if Grade=="A+"
    
    graph bar, over(Grade, sort(ordvar))
    The result is that I get this error message:

    Code:
    variable mean not found
    Is there an alternative way to do this that I am missing? I am convinced that there is a simple solution that I can't think of.

  • #2
    Update: Immediately after I posted this, I accidentally stumbled across this article by Nick Cox: https://core.ac.uk/display/6990279

    And this more or less solves the problem.

    Code:
    generate freq=1
    graph bar (percent) freq,  over(Grade, sort(ordvar))
    That said, I still don't really understand why the other version didn't work.

    Comment


    • #3
      Good question. I would send this in to StataCorp technical support.

      Comment

      Working...
      X