Announcement

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

  • ​​​​​Creating a graph with shared areas that cover positive and negative numbers in the y-axis

    Hello,

    I created a graph that includes markers for each one of the data points and shaded areas for periods. The chart looks fine when the plotted variable takes positive values. However, when it takes negative values, the shared areas are not shown for any negative value; they are just shown from zero to any positive value.

    The following is the code I am using:

    Code:
    preserve
        collapse var, by(group year)
        bysort group : gen first = _n == 1
        expand 2 if first, gen(newvar)
        replace year = 2008 if newvar == 1
        replace var = . if newvar == 1
        sort group year
        drop first newvar
        set scheme s1color
        separate var, by(group) veryshortlabel
        twoway scatteri -3 2002.5 1 2002.5 1 2005.5 -3 2005.5, recast(area) color(red*0.2) || ///
        scatteri -3 2005.5 1 2005.5 1 2007.5 -3 2007.5, recast(area) color(red*0.3) || ///
        scatter var? year, ms(dh oh) legend(lab(3 "A") lab(4 "B") order(4 3) position(6) col(1)) yscale(r(-3 1)) ylabel(-3(.5)1) xlabel(2000(1)2007)
    restore
    An example of the dataset I am using is:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(individual year group var)
     1 2000 1  -1.287641
     1 2001 1  -.8836126
     1 2002 1 -1.1622982
     1 2003 1  -1.082307
     1 2004 1 -1.1710584
     1 2005 1 -1.1796787
     1 2006 1 -1.2466114
     1 2007 1 -1.1606842
     2 2000 1 -.15043515
     2 2001 1  -.2209688
     2 2002 1 -.19620353
     2 2003 1  -.3402078
     2 2004 1  -.4571495
     2 2005 1  -.4485733
     2 2006 1 -.50437814
     2 2007 1  -.4616068
     3 2000 1  -.8550537
     3 2001 1  -.8602186
     3 2002 1  -.8605418
     3 2003 1  -.8876227
     3 2004 1  -.9352865
     3 2005 1 -1.0311842
     3 2006 1 -1.0150617
     3 2007 1 -1.0626712
     4 2000 1 -1.5768597
     4 2001 1 -1.4882183
     4 2002 1  -1.474381
     4 2003 1  -1.541484
     4 2004 1  -1.532406
     4 2005 1 -1.5681677
     4 2006 1 -1.4693305
     4 2007 1 -1.1766837
     5 2000 1  -.9059771
     5 2001 1  -.9684552
     5 2002 1 -1.0976796
     5 2003 1   -.921452
     5 2004 1  -.9920011
     5 2005 1  -1.285856
     5 2006 1 -1.2974216
     5 2007 1   -1.80346
     6 2000 1   .3173624
     6 2001 1  .25653982
     6 2002 1  .14316559
     6 2003 1  .13082898
     6 2004 1  .08094823
     6 2005 1 -.05380845
     6 2006 1  -.1298359
     6 2007 1    -.43694
     7 2000 1  -.8702447
     7 2001 1 -1.0644857
     7 2002 1 -1.0852207
     7 2003 1 -1.2581582
     7 2004 1 -1.1840831
     7 2005 1  -1.003717
     7 2006 1   -.875128
     7 2007 1  -.9327229
     8 2000 1 -1.3506677
     8 2001 1  -1.451754
     8 2002 1  -1.716019
     8 2003 1 -1.9588758
     8 2004 1 -2.0517306
     8 2005 1  -2.854429
     8 2006 1  -2.816115
     8 2007 1   -1.93434
     9 2000 1  -.4100389
     9 2001 1   -.435545
     9 2002 1 -.49928665
     9 2003 1  -.5177228
     9 2004 1   -.534188
     9 2005 1 -.58403265
     9 2006 1  -.6003072
     9 2007 1  -.5960971
    10 2000 0 -1.4216475
    10 2001 0 -1.4646496
    10 2002 0  -1.521221
    10 2003 0  -1.599065
    10 2004 0  -1.688853
    10 2005 0  -1.668002
    10 2006 0 -1.1764842
    10 2007 0  -1.364062
    end
    I appreciate any help you could provide.

    Thanks,

    Mayra


  • #2
    I think replacing the first two lines of your twoway command
    Code:
        twoway scatteri -3 2002.5 1 2002.5 1 2005.5 -3 2005.5, recast(area) color(red*0.2) || ///
        scatteri -3 2005.5 1 2005.5 1 2007.5 -3 2007.5, recast(area) color(red*0.3) || ///
    with
    Code:
        twoway scatteri -3 2002.5 1 2002.5 1 2005.5 -3 2005.5, recast(area) nodropbase color(red*0.2) || ///
        scatteri -3 2005.5 1 2005.5 1 2007.5 -3 2007.5, recast(area) nodropbase color(red*0.3) || ///
    will accomplish what you want. Experts on Stata Graphics may be able to tell us a better way of shading rectangular areas on a plot.

    Comment


    • #3
      It worked! Thanks, William!

      Comment

      Working...
      X