Announcement

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

  • sorting bar graph

    Dear all,

    here is my issue:

    I have three variables, call them x, y and z, with x + y = z, for thirty observations (countries).
    I would like to draw a bar graph which would show the structure of z, that is, I need stacked bars showing the respective percentages (shares) of x and y, and each bar's height is 100. x, y and z can all be negative.
    Now, I would like the bars to be sorted from the leftmost to the rightmost by the numerical value of x - not its share (percentage), but its exact numerical value - from the lowest to the highest.
    The problem is, when I submit

    Code:
    graph bar (asis) x y, over(countryname, sort(1)) stack percentages
    the bars get sorted by the percentage of x, and not its value.

    Thank you.

    Best,
    Ivica
    // ivica_rubil //

  • #2
    Let's re-create an example (ideally you should do this for us!) and give a solution:

    Code:
    . clear 
    
    . set obs 3
    obs was 0, now 3
    
    . gen name = word("Gondor Rohan Mordor", _n)
    
    . gen x = real(word("10 20 30", _n))
    
    . gen y = real(word("90 80 70", _n))
    
    . l
    
         +------------------+
         |   name    x    y |
         |------------------|
      1. | Gondor   10   90 |
      2. |  Rohan   20   80 |
      3. | Mordor   30   70 |
         +------------------+
    
    . graph bar (asis) x y, over(name) stack percent
    
    . sort x
    
    . gen axis = _n
    
    . labmask axis, values(name)
    
    . graph bar (asis) x y, over(axis) stack percent
    In words: create a variable specifying the exact order you want with value labels showing what you want to see.

    labmask is from SJ. A search labmask will reveal download location and a paper discussing it.

    Comment


    • #3
      Thank you, Nick.
      // ivica_rubil //

      Comment

      Working...
      X