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

  • Suppress part of legend when generating multiple twoway graphs

    Hi all,

    I'm a long-time reader, first-time poster. Thanks in advance for advice!

    I would like to superimpose a scatter plot and a line graph. In the legend, I'd like to label the scatter, but not the line. For example, this graph shows what I'd like to keep and what I'd like to suppress:

    sysuse auto, clear
    tab foreign, nol
    sysuse auto, clear
    reg price mpg if foreign
    predict phat_f
    reg price mpg if foreign==0
    predict phat_d
    twoway (scatter price mpg if foreign, mc(red) ytitle("Price") legend(label(1 "Foreign - keep this"))) ///
    (scatter price mpg if foreign==0, mc(blue) legend(label(2 "Domestic - and this"))) ///
    (line phat_f mpg, sort lc(red) legend(label(3 "Don't want this"))) ///
    (line phat_d mpg, sort lc(blue) legend(label(4 "or this")))

    My workaround has been to turn off the entire legend and add text within the plot region, but I'd like to know better.

    Thanks again,
    (using Stata 15)
    Last edited by William Burke; 16 Aug 2019, 11:34.

  • #2
    You want -legend(order())- :

    twoway scatter price mpg if foreign, mc(red) ytitle("Price") /// 
        || scatter price mpg if foreign==0, mc(blue)  ///
        || line phat_f mpg, sort lc(red)  ///
        || line phat_d mpg, sort lc(blue)  /// 
        ||, legend(order(1 "Foreign" 2 "Domestic"))


    • #3
      Yes, that's perfect - Thanks Scott!