Announcement

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

  • Creating a graph using xtline without connecting the data points. Shading some areas of the graph

    Hello,

    I am trying to create a graph using the command xtline. The graph contains a line for the variable y for each one of the three groups in my panel (group is the panel var). However, I am interested in creating a graph that shows the same information as what I would obtain using xtline, but without connecting the data points, just showing the points the way they are, and showing a different symbol for each one of the groups. I tried to use the option connect(none), but it seems to do not be an available option for xtline (or I did not know how to add it to the command).

    These are the instructions I am using to generate a graph that contains the three lines that plot the mean value of a variable "y" across time, for each of the groups.
    Code:
        collapse y [weight=weighta], by(group year)
        xtset group year
        xtline y,  overlay legend(lab(1 "Group1") lab(2 "Group2") lab(3 "Group3") position(6) order(3 1 2))
    If I include the option connect such as in the following lines, I obtain an error message.

    Code:
     xtline y, plot(connect(none)) overlay legend(lab(1 "Group1") lab(2 "Group2") lab(3 "Group3") position(6) order(3 1 2))
    connect(none) is not a twoway plot type
    In addition, I want to add two shaded areas to my graph. One should cover the period 2015-2015.5 and the other one 2015.5-2016. Is this possible to do?

    The following is a sample of my dataset:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(y weighta group) int year
    11.428572    10570 2 2010
    11.842364    10150 2 2011
    12.320623     9756 2 2012
    13.00975     9024 2 2013
    12.222222     9180 2 2014
    11.46325     8898 2 2015
    11.49895     9053 2 2016
    10.81656     6111 1 2010
    12.635822     5706 1 2011
    13.122416     5563 1 2012
    12.733035     5364 1 2013
    13.802083     4992 1 2014
    14.136683     4697 1 2015
    16.248613     4505 1 2016
    11.788158   128171 2 2010
    11.582245   122895 2 2011
    11.096654   114512 2 2012
    11.414516   109974 2 2013
    11.05669   107799 2 2014
    10.2708   101034 2 2015
    10.124253    94565 2 2016
    9.078657    24486 1 2010
    8.650405    21236 1 2011
    9.480315    19050 1 2012
    8.55654    17846 1 2013
    8.724371    18924 1 2014
    9.667835    20532 1 2015
    10.1494    22356 1 2016
    9.218224     5641 2 2010
    9.778789     4837 2 2011
    7.886231     4641 2 2012
    6.324973     4585 2 2013
    5.9375     4480 2 2014
    2.773284     4327 2 2015
    3.17662     3935 2 2016
    12.498936    35227 2 2010
    12.42346    34459 2 2011
    12.072604    33166 2 2012
    12.735373    30749 2 2013
    12.47237    30764 2 2014
    12.01376    30232 2 2015
    12.168823    29214 2 2016
    6.757194    12372 0 2010
    6.576466    12195 0 2011
    6.452705    11809 0 2012
    6.471396    10925 0 2013
    6.692101     9698 0 2014
    7.399577     8987 0 2015
    11.356073     9616 0 2016
    11.248214    20990 2 2010
    12.83428    19557 2 2011
    12.732884    17820 2 2012
    11.994135    17050 2 2013
    12.228617    17549 2 2014
    11.605978    16862 2 2015
    11.81248    16745 2 2016
    end
    Thanks in advance!

  • #2
    A line plot without connections sounds to me like ... a scatter plot. This may help:

    Reading list:

    https://www.stata-journal.com/sjpdf....iclenum=gr0023

    https://www.stata-journal.com/articl...article=gr0067

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(y weighta group) int year
    11.428572    10570 2 2010
    11.842364    10150 2 2011
    12.320623     9756 2 2012
    13.00975     9024 2 2013
    12.222222     9180 2 2014
    11.46325     8898 2 2015
    11.49895     9053 2 2016
    10.81656     6111 1 2010
    12.635822     5706 1 2011
    13.122416     5563 1 2012
    12.733035     5364 1 2013
    13.802083     4992 1 2014
    14.136683     4697 1 2015
    16.248613     4505 1 2016
    11.788158   128171 2 2010
    11.582245   122895 2 2011
    11.096654   114512 2 2012
    11.414516   109974 2 2013
    11.05669   107799 2 2014
    10.2708   101034 2 2015
    10.124253    94565 2 2016
    9.078657    24486 1 2010
    8.650405    21236 1 2011
    9.480315    19050 1 2012
    8.55654    17846 1 2013
    8.724371    18924 1 2014
    9.667835    20532 1 2015
    10.1494    22356 1 2016
    9.218224     5641 2 2010
    9.778789     4837 2 2011
    7.886231     4641 2 2012
    6.324973     4585 2 2013
    5.9375     4480 2 2014
    2.773284     4327 2 2015
    3.17662     3935 2 2016
    12.498936    35227 2 2010
    12.42346    34459 2 2011
    12.072604    33166 2 2012
    12.735373    30749 2 2013
    12.47237    30764 2 2014
    12.01376    30232 2 2015
    12.168823    29214 2 2016
    6.757194    12372 0 2010
    6.576466    12195 0 2011
    6.452705    11809 0 2012
    6.471396    10925 0 2013
    6.692101     9698 0 2014
    7.399577     8987 0 2015
    11.356073     9616 0 2016
    11.248214    20990 2 2010
    12.83428    19557 2 2011
    12.732884    17820 2 2012
    11.994135    17050 2 2013
    12.228617    17549 2 2014
    11.605978    16862 2 2015
    11.81248    16745 2 2016
    end
    
    collapse y [weight=weighta], by(group year)
    set scheme s1color 
    separate y, by(group) veryshortlabel 
    twoway scatteri 6 2015 12 2015 12 2015.5 6 2015.5, recast(area) color(red*0.2) || ///
    scatteri 6 2015.5 12 2015.5 12 2016 6 2016, recast(area) color(blue*0.2) || /// 
    scatter y? year, ms(Oh + Th) legend(order(5 4 3) position(3) col(1)) ytitle(mean whatever)
    Click image for larger version

Name:	xtline.png
Views:	1
Size:	20.4 KB
ID:	1483961

    Comment


    • #3
      This is exactly what I need. Thank you for the answer, Nick!

      Comment


      • #4
        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, which is very similar to the one Nick Cox shared with me in an older post (above):

        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

        Comment

        Working...
        X