Announcement

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

  • Dashed line graph for parts of the line

    Hello,

    I would like to use two line styles for the same graph to denote value above and below a certain threshold: twoway (tsline var if var<=1) (tsline var if var>1, lpattern(dash))

    I also tried creating two variables, one for values <=1 and another for values >1. But in both cases I run into the problem that - because the values for each variable to do not occur continuously - I get different shapes than if I were to just draw one line for all values. What I would like is to have the same shape as when I don't split it, but with a dashed line for values above 1.

    Could anyone suggest a solution?
    Many thanks!

  • #2
    Code:
    webuse grunfeld , clear
    
    line invest year if year <= 1945 & company == 1, lp(solid) || line invest year if year > 1945 & company ==1 , lp(dash)

    Comment


    • #3
      This is an interesting problem. In Nick Cox's example, there is a single crossing point and therefore, using year as a separator works. I gather that the OP is interested in situations where there exist multiple crossing points. As always, we advise that you provide reproducible examples (FAQ Advice #12). For me, therefore, this is a problem of interpolation. Below, I use mipolate from SSC by Nick Cox to illustrate a technique. The same dataset in #2 and the variable market value provide a reproducible example.

      Code:
      webuse grunfeld, clear
      keep if company==1
      keep mvalue year
      tsset year
      tsline mvalue, xtitle("") saving(gr1, replace)
      *ssc install mipolate
      gen points=100
      expand points, gen(new)
      sort year new
      replace mvalue=. if new
      gen time=_n
      mipolate mvalue time , gen(mv2)
      *SAY CUTOFF= MVALUE==4000. ID YEARS
      levelsof time if new==0
      gen mv3= cond(mv2> 4000, 4000, mv2)
      *GRAPH
      tw (line mv2 time, lp(dash) lc(black)) (line mv3 time, lc(black)) ///
      (scatteri 4000 0 4000 2001, recast(line) lw(thick) lc(white)) ///
      (scatteri 4000 0 4000 2001, recast(line) ls(grid) ///
      xlab(0 "1935" 500 "1940" 1000 "1945" 1500 "1950"  2000 "1955") ///
      saving(gr2, replace) leg(off))
      gr combine gr1.gph gr2.gph
      Click image for larger version

Name:	Graph.png
Views:	1
Size:	64.1 KB
ID:	1570823

      Comment


      • #4
        Thank you, both!

        Comment

        Working...
        X