Announcement

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

  • Clustered bar chart

    Hi,

    I am creating a barchart and have used the following code where var1 is a 4-category categorical variable and var2 is age in 3 categories

    Code:
    graph bar (percent), over(var1) over(var2) asyvars ///
        legend(order(1 "A" 2 "B" 3 "C" 4 "D") title("var1 category", size(small)) size(small)) ///
        note("Age in years", size(small) position(6)) ///
        ytitle("Percent") ///
        title("Distribution of var2 by three-category age")
    The graph looks how I would like. However, I can't work out how to get the bars to correspond to the within-category percentages, rather than the overall sample percentages. How do I do this on stata? Thank you

  • #2
    These problems are easier to tackle given a sight of a data example and of the graph you got.

    This may help as showing some technique:

    Code:
    clear
    set obs 100 
    set seed 314159
    
    gen age = runiformint(1, 3)
    label def age 1 young 2 middle 3 perfect 
    label val age age 
    
    gen four = runiformint(1, 4)
    
    graph bar (percent), over(four) over(age) asyvars ///
        legend(order(1 "A" 2 "B" 3 "C" 4 "D") title("var1 category", size(small)) size(small)) ///
        note("Age in years", size(small) position(6)) ///
        ytitle("Percent") ///
        title("Distribution of four by three-category age")
    
        
    graph bar (percent), over(four) by(age, note("") row(1) title("Distribution of four by three-category age")) ///
      asyvars legend(order(1 "A" 2 "B" 3 "C" 4 "D") title("var1 category", size(small)) size(small)) ///
        ytitle("Percent")

    Comment

    Working...
    X