Announcement

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

  • Adding tline to legend

    In a time series line graph I have added two dashed vertical lines to delineate dates of interest using the tline command (in the example below I've made one grey, one orange). I would like to include these dashed lines to the legend so the graph is self-contained, rather than having to explain what they mean outside the graph.

    I put together an example dataset to illustrate what I mean.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float mdate long outcome float value
    696 1  7
    697 1  6
    698 1  7
    699 1  8
    700 1  9
    701 1 11
    702 1 14
    703 1  7
    704 1  6
    705 1  6
    706 1  8
    707 1  7
    696 2 45
    697 2 46
    698 2 44
    699 2 42
    700 2 40
    701 2 37
    702 2 36
    703 2 45
    704 2 45
    705 2 44
    706 2 44
    707 2 46
    696 3 20
    697 3 20
    698 3 21
    699 3 21
    700 3 20
    701 3 21
    702 3 21
    703 3 20
    704 3 21
    705 3 21
    706 3 20
    707 3 20
    end
    format %tm mdate
    label values outcome outcomelbl
    label def outcomelbl 1 "var1", modify
    label def outcomelbl 2 "var2", modify
    label def outcomelbl 3 "var3", modify

    Here is the code for the graph:
    Code:
     
    tsset outcome mdate 
    twoway (tsline value if outcome==1) (tsline value if outcome==2) (tsline value if outcome==3), tline(2018m4, lp(dash) lc(grey) lw(thin)) tline(2018m8, lp(dash) lc(orange) lw(thin)) tlabel(#12) xlabel(, angle(45)) xlabel(, labsize(small))
    I have searched Statalist, Stata help and several instructional videos and the answer to what I thought was a fairly simple question is remaining elusive.

    Many thanks.

  • #2
    Thanks for the data example. See

    Code:
    help tw scatteri
    or

    Code:
    help tw dropline
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float mdate long outcome float value
    696 1  7
    697 1  6
    698 1  7
    699 1  8
    700 1  9
    701 1 11
    702 1 14
    703 1  7
    704 1  6
    705 1  6
    706 1  8
    707 1  7
    696 2 45
    697 2 46
    698 2 44
    699 2 42
    700 2 40
    701 2 37
    702 2 36
    703 2 45
    704 2 45
    705 2 44
    706 2 44
    707 2 46
    696 3 20
    697 3 20
    698 3 21
    699 3 21
    700 3 20
    701 3 21
    702 3 21
    703 3 20
    704 3 21
    705 3 21
    706 3 20
    707 3 20
    end
    format %tm mdate
    label values outcome outcomelbl
    label def outcomelbl 1 "var1", modify
    label def outcomelbl 2 "var2", modify
    label def outcomelbl 3 "var3", modify
    
    tsset outcome mdate
    qui sum value
    local min = r(min) - 0.12*r(max)
    local max = 1.12*r(max)
    set scheme s1color
    tw (tsline value if outcome==1) (tsline value if outcome==2) (tsline value if outcome==3) ///
    (scatteri `max' `=ym(2018,4)' `min' `=ym(2018,4)', recast(line) lp(dash) lc(grey) lw(thin)), ///
    xlab(, angle(45)) xlab(, labsize(small)) leg(order(1 2 3 4 "Whatever")) plotregion(margin(zero))
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	40.4 KB
ID:	1667988

    Last edited by Andrew Musau; 06 Jun 2022, 01:57.

    Comment


    • #3
      Thank you Andrew, that worked.

      Comment

      Working...
      X