Announcement

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

  • Adding xline into legend

    Hi,

    I've plotted the two-way graph with the xline to show the crisis period.

    I would like to add xline into the legend as well

    my code:

    Code:
    twoway (line wti date, yaxis(1) fc(gs10) lc(gs10) lpattern(shortdash)) || (line netwti date, yaxis(2) fc(black) lc(black)) ///
     if tin(01011988,01122019), title("") ytitle("Oil price (USD)", axis(1) size(small)) ytitle("Oil price return (%)", axis(2) size(small)) ///
     xline(11170 11201 11231 11262 11292, lwidth(thick) lc(ltblue)) ///
     xline(13849 13880 13911 13939 13970 14031 14092 14153 14214, lwidth(thick) lc(ltblue)) ///
     xline(15249 , lwidth(thick) lc(ltblue)) /// 9-11
     xline(15645 15675 15706 15737 , lwidth(thick) lc(ltblue)) /// Iraq war
     xline(17745 17806 17867 17898 , lwidth(thick) lc(ltblue)) ///
     xline(18659 18718 , lwidth(thick) lc(ltblue)) ///
     xline(19905 19936 19997 20058 20089 , lwidth(thick) lc(ltblue)) ///
     ylabel(,labsize(vsmall)) ylabel(,labsize(vsmall) axis(2)) xlabel(,labsize(vsmall)) xtitle("") ///
     graphregion(fcolor(white)) subtitle(,lcolor(white) color(black)) legend( label (1 "Oil price") label (2 "Oil price return") size(small) region(col(white))) xlabel(#15,grid) ylabel(, nogrid)  ///
     tlabel(, format(%tdCY)) ylabel(15(15)140) ylabel(-40(10)40, axis(2))

    Click image for larger version

Name:	testest.png
Views:	1
Size:	81.2 KB
ID:	1571213



    Now, legend only show dash line (oil price) and black line (oil price return). I'm wondering how can the xline (blue color) into the legend?

    Thank you



  • #2
    -xline- is an option. Only tw plots can appear in the legend. You can add a line using scatteri or dropline.

    Code:
    webuse grunfeld, clear
    tw (line invest year if company== 8, scheme(s1color)) ///
    (scatteri 0 1945 100 1945, recast(line) lwidth(vvvthick) lc(ltblue%70))
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	34.9 KB
ID:	1571217

    Comment

    Working...
    X