Search code examples
stata

How to Change Color of Symbols in Legend for Two-Way Lowess


I'm trying to change the color of the two lines in the legend for a two-way lowess plot.

My code is as follows:

twoway (lowess y x if z == 0) (lowess y x if z == 1), ///  
        title("Probabilistic Relationship between Incidence of Y and Diagnosis of X") ///  
        ytitle(Probability of Incidence of Y)                  ///  
        ylabel(0.0 0.05 0.10 0.15 0.20)                            ///  
        xtitle(Diagnosis of X)                      ///  
        legend(label(1 "Non-Z") label(2 "Current Z")) ///  
        rline(lcolor(1 "eltblue") (2 "eltgreen"),                       ///  
        lstyle(1 "solid") (2 "solid"))                          ///  
        note("Anonymous Project")  

I keep getting errors on various lines of code, particular for rline and lstyle. What options am I supposed to use for twoway lowess to edit the color of the lines in the plot and correspondingly, the legend? The lcolor and rline options don't work. Please guide me on how to navigate this issue!


Solution

  • Here's a reproducible example:

    sysuse auto
    tw (lowess price mpg if foreign==1, lcolor(orange)) (lowess price mpg if foreign==0, lcolor(navy)), legend(label(1 "Foreign Cars") label(2 "Domestic Cars")) 
    

    Here's the output:

    enter image description here