Announcement

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

  • Lines on scatterplot: how not to include them in the legend?

    Hello,

    I want to add some lines to divide a scatterplot in zones, e.g. via twoway pci.
    See example below:
    Code:
    sysuse auto
    scatter price mpg, xline(22.5) || (pci 10000 22.5 10000 42)
    However, on the graph, I only want the legend for the dots ("Price") to appear below the graph, and not the "y/yb" for the horizontal line.
    I tried the following command, but then the entire legend disappears ...
    Code:
    twoway (scatter price mpg, xline(22.5) legend(on)) || (pci 10000 22.5 10000 42, legend(off))
    Does anyone has an idea how to solve this issue?

    Click image for larger version

Name:	Fig.png
Views:	1
Size:	50.5 KB
ID:	1587627



  • #2
    There are no multiple legends in a twoway graph, so you need the -order()- suboption to include or exclude plots.

    Code:
    legend(order(1))

    Comment


    • #3
      Wow, works fantastic!
      Thank you very much.

      Comment


      • #4
        I would omit the legend here. Once you add ytitle(Price (USD)) it is superfluous. Very likely you would want to explain the added lines in a caption in Word or LaTeX or whatever, but that's a different story.

        Comment


        • #5
          Nick Cox The example I provided in #1 was indeed a bit too simplistic (with a superfluous legend) ... My actual problem was more something like this:
          Code:
          sysuse auto
          separate price, by(foreign) shortlabel
          scatter price? mpg, xline(22.5) || (pci 10000 22.5 10000 42)
          resulting in this graph

          Click image for larger version

Name:	Fig1.png
Views:	1
Size:	59.4 KB
ID:	1587681



          The solution proposed in #2 ...
          Code:
          scatter price? mpg, xline(22.5) || (pci 10000 22.5 10000 42), legend(order(1 2))
          ... results in this graph (what I was looking for):


          Click image for larger version

Name:	Fig2.png
Views:	1
Size:	59.0 KB
ID:	1587682



          Comment


          • #6
            I suggest

            Code:
             
             separate price, by(foreign) veryshortlabel
            as explained in https://www.stata-journal.com/sjpdf....iclenum=gr0023 (see also https://www.statalist.org/forums/for...lable-from-ssc).

            Comment


            • #7
              That's much nicer, indeed ... Thank you.

              Comment

              Working...
              X