Announcement

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

  • Create Serrbars with Multiple Categories in one Chart

    Hi,

    I'm trying to make a chart with standard errors over time, over two categories.
    I've managed to make one chart for each with the following commands

    serrbar politye see t if outcome3==1, scale (1.96) name(se1, replace)
    serrbar politye see t if outcome3==0, scale (1.96) name(se0, replace)

    (so outcome3 is the category, politye the mean, see the standard error, t the time variable)

    I would like to combine the two into one chart. However serrbar does not allow the by() option. Graph combine gives me two charts next to each other, and I can't use twoway because serrbar is not twoway.

    What should I do?

    Thank you!

  • #2
    serrbar isn't a big deal. It's just a wrapper for some simple twoway calls. It does support addplot() -- compare e.g. https://www.statalist.org/forums/for...serrbar-graphs -- or you could just write your own twoway code.

    You don't provide example code (do please read and act on FAQ Advice #12), but here is token code.

    Code:
    clear
    set scheme s1color 
    input outcome3 t politye see 
    0  1990    4   0.1
    0  2010    5   0.1 
    1  1990    6   0.2
    1  2010    7   0.3 
    end 
    
    gen upper = politye + 1.96 * see 
    gen lower = politye - 1.96 * see 
    
    twoway rcap upper lower t if outcome3 == 1, lc(red) /// 
    ||     rcap upper lower t if outcome3 == 0, lc(blue) /// 
    ||     scatter politye t if outcome3 == 1, mc(red)  /// 
    ||     scatter politye t if outcome3 == 0, mc(blue) ///
    ytitle(politye) legend(order(3 "silly outcome" 4 "sensible outcome")) ///
    xla(1990 2010) xsc(r(1985 2015)) yla(, ang(h)) xtitle("")
    Click image for larger version

Name:	serrbar.png
Views:	1
Size:	17.0 KB
ID:	1421880

    Comment


    • #3
      Correction to #2, twoway serrbar gives error
      serrbar is not a twoway plot type
      .

      For anyone wanting to plot the same using age groups, I append the code here.
      HTML Code:
          collapse (mean) `y'_mean = `y' (sd) `y'_sd = `y' if !`t', by(`agegrp') // category t==0
          gen `t'=0
          app     using `firstdata' // append the first data above
      gen upper = `y'_mean + 1.96 * `y'_sd 
      gen lower = `y'_mean - 1.96 * `y'_sd
      twoway rcap upper lower `agegrp'     if `t' == 1, lc(red) /// 
      ||     rcap upper lower `agegrp'     if `t' == 0, lc(blue) /// 
      ||     scatter `y'_mean `agegrp'     if `t' == 1, mc(red)  /// 
      ||     scatter `y'_mean `agegrp'     if `t'== 0, mc(blue) ///
      ytitle(`y'_mean) legend(order(1 "`t'=1" 2 "`t'=0")) ///
      xla(45(5)80) xsc(r(43 82)) yla(, ang(h)) xtitle("") scheme(s1color)
      The only imperfection I get from this graph is that the scheme s1color is not applied. There seems to be an issue here. However, in the example code above posted by Nick, setting the scheme also works.

      Comment


      • #4
        Castor Comploj

        That's not a correction. I didn't say that serrbar was a twoway type; it's a separate command.

        The code in #3 depends on local macros that aren't defined anywhere within it. I stop there, as I can't apply the code.

        Comment

        Working...
        X