Announcement

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

  • Change order of bars using twoway bar

    Hi Statalist

    Is there anyway of adjusting this graph such that the two estimates for “low” are next to each other, the two estimates for “mod” are next to each other, and the two estimates for “high” are next to each other. At the moment the graph is ordered as low, mod, high, low, mod, high - instead of low, low, mod, mod, high, high.

    I am not sure how to edit my code to achieve this. Thank you for any help.

    Code:
    gen foodh = foodsec if hyb==1
    replace foodh = foodsec+5 if hyb==2
    
    sort foodh
    list foodh foodsec hyb, sepby(foodsec)
    
    twoway (bar estimate foodh if hyb==1) (bar estimate foodh if hyb==2) (rcap up lw foodh), legend(order(1 "between" 2 "within")) xlabel(1 "Low" 2 "Mod" 3 "High" 6 "Low" 7 "Mod" 8 "High", noticks) legend(pos(6))
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input long(country hyb) byte foodsec double(estimate lw up) float foodh
    2 1 1 .65 .62 .68 1
    2 1 2 .75 .73 .77 2
    2 1 3 .83 .79 .87 3
    2 2 1 .72  .7 .73 6
    2 2 2 .77 .72 .81 7
    2 2 3 .81 .73 .88 8
    end
    label values country country
    label def country 2 "Jordan", modify
    label values hyb hyb
    label def hyb 1 "between", modify
    label def hyb 2 "within", modify
    label values foodsec fd
    label def fd 1 "Low", modify
    label def fd 2 "Mod", modify
    label def fd 3 "High", modify

  • #2
    You need a new x variable. See also https://www.stata-journal.com/articl...article=gr0034 and https://www.stata-journal.com/articl...article=gr0053


    Code:
    clear 
    input long(country hyb) byte foodsec double(estimate lw up) float foodh 
    2 1 1 .65 .62 .68 1 
    2 1 2 .75 .73 .77 2
    2 1 3 .83 .79 .87 3
    2 2 1 .72  .7 .73 6
    2 2 2 .77 .72 .81 7
    2 2 3 .81 .73 .88 8
    end
    label values country country
    label def country 2 "Jordan", modify
    label values hyb hyb
    label def hyb 1 "between", modify
    label def hyb 2 "within", modify
    label values foodsec fd
    label def fd 1 "Low", modify
    label def fd 2 "Mod", modify
    label def fd 3 "High", modify
    
    * gen foodh = cond(hyb == 1, foodsec, foodsec+5) 
    
    sort foodh
    list foodh foodsec hyb, sepby(foodsec)
    
    gen newx = hyb + 3 * (foodsec - 1)  
    
    twoway (bar estimate newx if hyb==1, base(0)) (bar estimate newx if hyb==2, base(0)) (rcap up lw newx), legend(order(1 "between" 2 "within")) xlabel(1 "Low" 2 "Low" 4 "Mod" 5 "Mod" 7 "High" 8 "High", noticks) legend(pos(12)) xtitle(Food security)

    Comment

    Working...
    X