Announcement

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

  • Getting two categories of var onto same bar plot

    Hello, I am making a bar plot of average qA_total scores by week, and I also have a variable (attendance) that is either "High" or "Low".

    I have made two separate bar plots for just the "High" group and just the "Low" group by just keeping one value or the other, but am trying to figure out a way to get them both onto the same plot side-by-side.

    Here is some code and data (there is a bit of extra code before the plot just to overlay error bars on there)

    Code:
    collapse(mean) meanqA_total = qA_total (sd) sdqA_total = qA_total (count) n = qA_total, by(week)
    gen hiqA_total = meanqA_total + invttail(n-1,0.025)*(sdqA_total / sqrt(n))
    gen loqA_total = meanqA_total - invttail(n-1,0.025)*(sdqA_total / sqrt(n))
    twoway(bar meanqA_total week) (rcap hiqA_total loqA_total week)

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(qA_total week) str4 attendance
    . 4 "High"
    1 4 "High"
    3 4 "Low"
    3 4 "Low"
    2 4 "Low"
    3 4 "High"
    3 1 "Low"
    3 1 "High"
    3 1 "High"
    1 1 "High"
    1 1 "High"
    1 1 "High"
    3 1 "Low"
    3 2 "High"
    1 2 "High"
    2 2 "High"
    2 2 "High"
    2 2 "High"
    2 2 "High"
    2 2 "High"
    2 3 "High"
    3 3 "High"
    3 3 "High"
    2 3 "High"
    2 3 "High"
    1 3 "High"
    . 3 "Low"
    . 4 "High"
    3 4 "Low"
    3 4 "Low"
    3 4 "Low"
    3 4 "High"
    1 4 "High"
    2 4 "High"
    1 4 "High"
    2 1 "High"
    3 1 "High"
    2 1 "High"
    3 1 "High"
    2 1 "High"
    3 1 "High"
    3 1 "High"
    . 2 "High"
    . 2 "High"
    . 2 "High"
    . 2 "High"
    . 2 "High"
    . 2 "Low"
    2 2 "Low"
    2 3 "Low"
    2 3 "Low"
    3 3 "High"
    3 3 "High"
    4 3 "High"
    5 3 "High"
    2 3 "High"
    3 4 "High"
    3 4 "High"
    1 4 "High"
    1 4 "High"
    3 4 "High"
    1 4 "High"
    3 4 "High"
    3 4 "High"
    2 1 "High"
    2 1 "High"
    3 1 "High"
    3 1 "Low"
    1 1 "High"
    3 1 "High"
    . 1 "Low"
    . 2 "High"
    . 2 "High"
    1 2 "High"
    1 2 "High"
    1 2 "High"
    1 2 "High"
    0 2 "High"
    . 3 "High"
    . 3 "High"
    . 3 "High"
    . 3 "High"
    . 3 "High"
    . 3 "High"
    3 3 "High"
    3 4 "High"
    3 4 "High"
    3 4 "High"
    3 4 "High"
    3 4 "High"
    3 4 "Low"
    3 4 "High"
    3 4 "Low"
    3 4 "Low"
    3 4 "High"
    3 4 "High"
    3 1 "Low"
    3 1 "Low"
    1 1 "High"
    0 1 "High"
    end

  • #2
    Code:
    statsby, by(week attendance) clear : ci means qA_total 
    
    twoway scatter mean week, by(attendance, note("") subtitle(qA_total means and 95% confidence intervals) legend(off)) || rcap ub lb week , yla(, ang(h))
    For scatter read bar if so inclined. But Google plunger plot, dynamite and detonator plot for discussions of how this form is overrated. It seems that your variable is small integers, so histograms alongside might be good.

    Code:
    twoway bar mean week, base(0) barw(0.5) bfcolor(none) by(attendance, note("") subtitle(qA_total means and 95% confidence intervals) legend(off)) || rcap ub lb week , xla(1/4) yla(, ang(h))

    Comment


    • #3
      Thanks Nick Cox, this histogram looks great. Is there a way to make it so the Week 1 bars are directly next to each other (one high, one low), then Week 2 bars next to them, etc. all on one individual plot together?

      Comment


      • #4
        Not a histogram! But surely, just change the roles of the variables.


        Code:
        * not tested
        
        label def which 1 High 2 Low
        encode attendance, gen(which) label(which)
          
         twoway bar mean which, base(0) barw(0.5) bfcolor(none) by(week, note("") subtitle(qA_total means and 95% confidence intervals) legend(off)) ///
        || rcap ub lb which , xla(1/2, valuelabel) yla(, ang(h))

        Comment

        Working...
        X