Announcement

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

  • Multiple variable catplot by location

    Hi,

    I'm trying to replicate the following excel-generated graph on stata (and horizontally), but was unable to do so as:
    - when using graph hbar var1-var7, by(location), I would get two graphs, combined;
    - when using graph hbar var1-var7, over(location), I would first get all bars for var1-var7 for the first location, then again for the second location.

    Var1-Var7 are generated from a select-multiple survey variable, dichotomized for each potential response option.

    Thanks

    Click image for larger version

Name:	excel graph.jpg
Views:	1
Size:	16.5 KB
ID:	1689906

  • #2
    There is no data example here, but here is some technique. To get vertical bars, you need graph bar.

    Code:
    clear 
    set obs 100 
    set seed 2022
    gen location = 1 + (_n > 50)  
    forval j = 1/7 { 
    gen var`j' = runiformint(0, 10) 
    } 
    
    * start here 
    preserve 
    
    gen id = _n
    reshape long var, i(id) j(which) 
    separate var, by(location) veryshortlabel 
    
    graph bar (mean) var? , over(which) bar(2, color(orange)) bar(1, color(blue)) ///
    legend(order(1 "Location 1" 2 "Location 2"))
    
    
    restore

    Comment


    • #3
      Perfect, thank you Nick Cox

      Comment

      Working...
      X