Announcement

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

  • With xtline command, can I generate plot lines that are solid up to a certain year and then becomes dotted?

    I would like to show the trend before and after a certain treatment year on each line. To be more clear, for instance I would like to have a line for the treatyear==2008 (the red line) that becomes dotted after 2008.

  • #2
    I am not familiar with the xtline command, and I cannot read your photo (please read the FAQ), but wanting this kind of line structure is not unusual; the "trick" is to let Stata believe that it is making two lines that meet somewhere (use "if" or "in" or "range" - whatever is relevant) and having the type of line be different for each horizontal piece (e.g., solid up to 2008 and dotted after 2008)

    Comment


    • #3
      Thank you very much for your suggestion. It is a nice trick but it will require me to generate two variables for each id in the panel. However, the xtline command does not allow more than one variable with the overlay option. Something like "xtline X1 X2, overlay" is not possible.

      Comment


      • #4
        Rich is completely correct. There is absolutely no need to create new variables or even to use xtline

        This reproducible example appears of similar complexity to your data, which we can't use because you don't give it.


        Code:
        webuse grunfeld, clear
        set scheme s1color
        keep invest company year
        keep if company <= 4
        
        tokenize red blue black magenta
        
        local call
        
        forval j = 1/4 {
            local call `call' line invest year if co == `j' & year <= 1945, lp(solid) lc(``j'') ||
            local call `call' line invest year if co == `j' & year >= 1945, lp(dash) lc(``j'') ||
        }
        
        `call' line invest year, lc(none) ///
        legend(order(1 "1" 3 "2" 5 "3" 7 "4") ring(0) col(1) pos(5)) ysc(log) yla(30 100 300 1000, ang(h))
        The basic idea is a little tedious to implement but not difficult. You need a separate call for each panel and each period. Code like that above makes it a little easier to build up the syntax in a loop but does require introductory knowledge of local macros. But it is not essential: you can just build up the code in an editor window making use of copy and paste.

        Personally I'd recommend dashes not dots. Dots don't always reproduce well across software or into papers or presentations and they are rather subdued for anything of real interest.

        Click image for larger version

Name:	linedash.png
Views:	1
Size:	12.4 KB
ID:	1326761
        Last edited by Nick Cox; 13 Feb 2016, 12:08.

        Comment


        • #5
          Thank you very much for the detailed explanation and code. This is the graph I was exactly looking for but I need specific turning points for each id like id1 becomes dashed after 1940 whereas the id 2 after 1950. Since I don't have knowledge of local macros at all, I tried but was not able to go further on this code to do that. I would appreciate if you can code this, too.

          Comment


          • #6
            As said, you don't need to use local macros. You can just write a series of calls that resemble

            Code:
             
             line invest year if co == 1 & year <= 1945, lp(solid) lc(red)  || line invest year if co == 1 & year >= 1945, lp(dash) lc(red)  || <more> || <more>

            Comment


            • #7
              Finally did it! Thank you so much.

              Comment

              Working...
              X