Announcement

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

  • Graph twoway line by group: adding a different vertical line for each group

    I'm using twoway line to create graphs of earnings by year. I want to create separate graphs for 20 individuals. For each graph, I want to create a vertical line for the year in which that individual left the program. The year will differ by individual, and is captured by the variable lastyear2.

    Here is the syntax without the vertical line part:

    twoway (line wage_year2 acadyear, sort) if sample2<=20 & award==1, ///
    ytitle(Annual earnings) ytitle(, size(small)) ylabel(, angle(horizontal) format(%9.0fc)) ///
    xtitle(Year) xtitle(, size(small)) ///
    by(, title(Earnings for a Random Sample of Program Completers, size(medsmall)) ///
    note("")) ///
    by(sample2)


    I think xline won't work because it wants a number, and doesn't seem to allow a separate value for each individual.

    scatteri also doesn't allow an argument that varies by individual.

    I tried using the function command, but I'm not sure function can graph a vertical line.

    A subset of the data is below.

    +------------------------------------------+
    sample2 acadyear wage_y~2 lastye~2

    1 2010 5000 2013
    1 2011 7000 2013
    1 2012 2000 2013
    1 2013 33000 2013
    1 2014 0 2013
    1 2015 0 2013

    2 2008 10000 2014
    2 2009 13000 2014
    2 2010 14000 2014
    2 2011 13000 2014
    2 2012 9000 2014
    2 2013 15000 2014
    2 2014 47000 2014
    2 2015 52000 2014

    3 2010 25000 2014
    3 2011 28000 2014
    3 2012 21000 2014
    3 2013 20000 2014
    3 2014 0 2014
    3 2015 5000 2014

    4 2009 4000 2012
    4 2010 20000 2012
    4 2011 17000 2012
    4 2012 6000 2012
    4 2013 0 2012
    4 2014 0 2012
    4 2015 0 2012

    5 2008 2000 2014
    5 2009 1000 2014
    5 2010 0 2014
    5 2011 7000 2014
    5 2012 9000 2014
    5 2013 0 2014
    5 2014 12000 2014
    5 2015 54000 2014

    6 2009 0 2014
    6 2010 1000 2014
    6 2011 4000 2014
    6 2012 3000 2014
    6 2013 0 2014
    6 2014 19000 2014
    6 2015 60000 2014

    7 2009 0 2014
    7 2010 0 2014
    7 2011 28000 2014
    7 2012 29000 2014
    7 2013 30000 2014
    7 2014 41000 2014
    7 2015 17000 2014

    8 2009 11000 2014
    8 2010 2000 2014
    8 2011 0 2014
    8 2012 4000 2014
    8 2013 10000 2014
    8 2014 0 2014
    8 2015 0 2014

    9 2008 0 2010
    9 2009 0 2010
    9 2010 30000 2010
    9 2011 36000 2010
    9 2012 35000 2010
    9 2013 44000 2010
    9 2014 45000 2010
    9 2015 56000 2010

    10 2010 5000 2012
    10 2011 5000 2012
    10 2012 12000 2012
    10 2013 11000 2012
    10 2014 22000 2012
    10 2015 29000 2012

    11 2009 3000 2013
    11 2010 0 2013
    11 2011 0 2013
    11 2012 0 2013
    11 2013 42000 2013
    11 2014 61000 2013
    11 2015 27000 2013

    12 2008 4000 2016
    12 2009 4000 2016
    12 2010 11000 2016
    12 2011 15000 2016
    12 2012 22000 2016
    12 2013 22000 2016
    12 2014 14000 2016
    12 2015 38000 2016

    13 2008 19000 2010
    13 2009 16000 2010
    13 2010 40000 2010
    13 2011 38000 2010
    13 2012 42000 2010
    13 2013 46000 2010
    13 2014 47000 2010
    13 2015 46000 2010

    14 2009 1000 2011
    14 2010 0 2011
    14 2011 0 2011
    14 2012 0 2011
    14 2013 0 2011
    14 2014 0 2011
    14 2015 0 2011

    15 2009 0 2012
    15 2010 1000 2012
    15 2011 5000 2012
    15 2012 21000 2012
    15 2013 11000 2012
    15 2014 0 2012
    15 2015 0 2012

    16 2010 0 2013
    16 2011 0 2013
    16 2012 0 2013
    16 2013 37000 2013
    16 2014 38000 2013
    16 2015 29000 2013

    17 2008 22000 2011
    17 2009 6000 2011
    17 2010 20000 2011
    17 2011 47000 2011
    17 2012 46000 2011
    17 2013 44000 2011
    17 2014 53000 2011
    17 2015 64000 2011

    18 2009 1000 2014
    18 2010 0 2014
    18 2011 6000 2014
    18 2012 8000 2014
    18 2013 11000 2014
    18 2014 12000 2014
    18 2015 6000 2014

    19 2010 8000 2013
    19 2011 10000 2013
    19 2012 9000 2013
    19 2013 35000 2013
    19 2014 46000 2013
    19 2015 48000 2013

    20 2009 7000 2011
    20 2010 5000 2011
    20 2011 24000 2011
    20 2012 36000 2011
    20 2013 32000 2011
    20 2014 6000 2011
    20 2015 0 2011




  • #2
    Maybe a third approach base on

    Code:
    sysuse auto
    bysort foreign, egen max_weight = max(weight)
    summarize max_weight if foreign, meanonly
    graph twoway scatter length weight if foreign, xline(`r(mean)')
    summarize max_weight if !foreign, meanonly
    graph twoway scatter length weight if !foreign, xline(`r(mean)')
    You could wrap something like this in a foreach loop to get the levels of each individual.

    Comment


    • #3
      With a little surgery your data and code allow a reproducible example. Please note the use of CODE delimiters.


      Code:
      clear 
      input sample2 acadyear wage_year2 lastyear2 
      1 2010 5000 2013 
      1 2011 7000 2013 
      1 2012 2000 2013 
      1 2013 33000 2013 
      1 2014 0 2013 
      1 2015 0 2013 
      2 2008 10000 2014 
      2 2009 13000 2014 
      2 2010 14000 2014 
      2 2011 13000 2014 
      2 2012 9000 2014 
      2 2013 15000 2014 
      2 2014 47000 2014 
      2 2015 52000 2014 
      3 2010 25000 2014 
      3 2011 28000 2014 
      3 2012 21000 2014 
      3 2013 20000 2014 
      3 2014 0 2014 
      3 2015 5000 2014 
      4 2009 4000 2012 
      4 2010 20000 2012 
      4 2011 17000 2012 
      4 2012 6000 2012 
      4 2013 0 2012 
      4 2014 0 2012 
      4 2015 0 2012 
      5 2008 2000 2014 
      5 2009 1000 2014 
      5 2010 0 2014 
      5 2011 7000 2014 
      5 2012 9000 2014 
      5 2013 0 2014 
      5 2014 12000 2014 
      5 2015 54000 2014 
      6 2009 0 2014 
      6 2010 1000 2014 
      6 2011 4000 2014 
      6 2012 3000 2014 
      6 2013 0 2014 
      6 2014 19000 2014 
      6 2015 60000 2014 
      7 2009 0 2014 
      7 2010 0 2014 
      7 2011 28000 2014 
      7 2012 29000 2014 
      7 2013 30000 2014 
      7 2014 41000 2014 
      7 2015 17000 2014 
      8 2009 11000 2014 
      8 2010 2000 2014 
      8 2011 0 2014 
      8 2012 4000 2014 
      8 2013 10000 2014 
      8 2014 0 2014 
      8 2015 0 2014 
      9 2008 0 2010 
      9 2009 0 2010 
      9 2010 30000 2010 
      9 2011 36000 2010 
      9 2012 35000 2010 
      9 2013 44000 2010 
      9 2014 45000 2010 
      9 2015 56000 2010 
      10 2010 5000 2012 
      10 2011 5000 2012 
      10 2012 12000 2012 
      10 2013 11000 2012 
      10 2014 22000 2012 
      10 2015 29000 2012 
      11 2009 3000 2013 
      11 2010 0 2013 
      11 2011 0 2013 
      11 2012 0 2013 
      11 2013 42000 2013 
      11 2014 61000 2013 
      11 2015 27000 2013 
      12 2008 4000 2016 
      12 2009 4000 2016 
      12 2010 11000 2016 
      12 2011 15000 2016 
      12 2012 22000 2016 
      12 2013 22000 2016 
      12 2014 14000 2016 
      12 2015 38000 2016 
      13 2008 19000 2010 
      13 2009 16000 2010 
      13 2010 40000 2010 
      13 2011 38000 2010 
      13 2012 42000 2010 
      13 2013 46000 2010 
      13 2014 47000 2010 
      13 2015 46000 2010 
      14 2009 1000 2011 
      14 2010 0 2011 
      14 2011 0 2011 
      14 2012 0 2011 
      14 2013 0 2011 
      14 2014 0 2011 
      14 2015 0 2011 
      15 2009 0 2012 
      15 2010 1000 2012 
      15 2011 5000 2012 
      15 2012 21000 2012 
      15 2013 11000 2012 
      15 2014 0 2012 
      15 2015 0 2012 
      16 2010 0 2013 
      16 2011 0 2013 
      16 2012 0 2013 
      16 2013 37000 2013 
      16 2014 38000 2013 
      16 2015 29000 2013 
      17 2008 22000 2011 
      17 2009 6000 2011 
      17 2010 20000 2011 
      17 2011 47000 2011 
      17 2012 46000 2011 
      17 2013 44000 2011 
      17 2014 53000 2011 
      17 2015 64000 2011 
      18 2009 1000 2014 
      18 2010 0 2014 
      18 2011 6000 2014 
      18 2012 8000 2014 
      18 2013 11000 2014 
      18 2014 12000 2014 
      18 2015 6000 2014 
      19 2010 8000 2013 
      19 2011 10000 2013 
      19 2012 9000 2013 
      19 2013 35000 2013 
      19 2014 46000 2013 
      19 2015 48000 2013 
      20 2009 7000 2011 
      20 2010 5000 2011 
      20 2011 24000 2011 
      20 2012 36000 2011 
      20 2013 32000 2011 
      20 2014 6000 2011 
      20 2015 0 2011 
      end 
      
      gen top = 60000
      gen zero = 0 
      
      twoway line wage_year2 acadyear, sort  ///
      ytitle(Annual earnings, size(small)) ylabel(, ang(h) format(%9.0fc)) ///
      xtitle(Year, size(small)) ///
      by(sample2, title(Earnings for a Random Sample of Program Completers, size(medsmall)) ///
      note("") legend(off)) ///
      || rspike zero top lastyear2 , lw(thin)

      Comment


      • #4
        Please use -dataex- when posting data. It makes things so much easier and improves the odds that someone will help you.

        I would probably loop over each observation and combine the graphs at the end like this:

        Code:
        cls
        clear
        input sample acadyear wage_year2 lastyear2 
        1 2010 5000 2013 
        1 2011 7000 2013 
        1 2012 2000 2013 
        1 2013 33000 2013 
        1 2014 0 2013 
        1 2015 0 2013 
        2 2008 10000 2014 
        2 2009 13000 2014 
        2 2010 14000 2014 
        2 2011 13000 2014 
        2 2012 9000 2014 
        2 2013 15000 2014 
        2 2014 47000 2014 
        2 2015 52000 2014 
        3 2010 25000 2014 
        3 2011 28000 2014 
        3 2012 21000 2014 
        3 2013 20000 2014 
        3 2014 0 2014 
        3 2015 5000 2014 
        4 2009 4000 2012 
        4 2010 20000 2012 
        4 2011 17000 2012 
        4 2012 6000 2012 
        4 2013 0 2012 
        4 2014 0 2012 
        4 2015 0 2012 
        end
        
        levelsof sample, clean local(my_obs)
        
        local graph_list ""
        
        foreach i of local my_obs {
            sum lastyear2 if sample ==`i', meanonly
            tw line wage_year2 acadyear if sample==`i', xline(`r(mean)') name(g`i', replace)
            local graph_list "`graph_list' g`i'"
        }
        
        graph combine `graph_list'

        Comment


        • #5
          Thanks very much to Nick for a simple solution, to Dmitriy for his alternative and for letting me know about -dataex-. And thanks also to Dave for his suggestion.

          Comment

          Working...
          X