Announcement

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

  • Graph legend placement in ring(0) with by()

    I'm trying to produce a side-by-side twoway scatter plot using by(). I want to place the legend inside one of the two scatter plots, but am only succeeding in moving the legend around outside the plots.

    Here's a simplified version of the graphing code:

    twoway (scatter mean grade if fips==1&cohort==2015) ///
    (scatter mean grade if cohort==2015 & fips==2), ///
    by(subject) leg(ring(0) al(bottom) j(right))



    (Note - I'm aware I can get to a solution with graph combine or grc1leg, but I'm trying to stick with the current framework for automation reasons.)

  • #2
    Could you please provide a reproducible example or at least show your graph? You refer to "one of the two scatter plots" but a command like the one in your post can create a graph with any number of plots, as this example shows:
    Code:
    sysuse auto, clear
    twoway (scatter mpg weight if foreign==0) (scatter mpg weight if foreign==1), by(rep78)
    Please read the FAQ, especially section 12, for some advice on using Statalist. The more you tell us about your problem, the easier it is for others to help.

    Comment


    • #3
      Friedrich gives excellent advice but more can be said. Looking at your syntax

      Code:
      twoway (scatter mean grade if fips==1&cohort==2015) (scatter mean grade if cohort==2015 & fips==2), by(subject) leg(ring(0) al(bottom) j(right))
      all data refer to the 2015 cohort and what distinguishes the two scatters is being fips 1 or 2. So, you could have self-explanatory symbols:

      Code:
      gen one = 1 
      gen two = 2 
      twoway (scatter mean grade if fips==1&cohort==2015, mla(one) mlabpos(0) mlcolor(red) ms(none) ) 
      (scatter mean grade if cohort==2015 & fips==2) , mla(two) mlabpos(0) mlcolor(blue) ms(none), by(subject)
      save the valuable graph real estate for showing the data more simply and directly.


      Comment


      • #4
        Thanks. Yes, the choices about layout are being driven by a broader set of considerations about consistency across multiple plots, and the code presented was heavily simplified (i.e. the cleaner presentation suggested is cluttered once I add everything that's actually going on). Here's a reproducible version:

        sysuse auto, clear
        twoway (scatter price mpg if rep78<=3) (scatter price mpg if rep78>3), by(foreign) leg(ring(0) pos(2))

        The goal is to move the legend inside the plot area for one of the two graphs. If there isn't a way to do that, I can make graph combine work - the code is just (much) more efficient if there's a way to make it work with the by() option.

        Comment


        • #5
          Sorry; what you want makes sense, but I don't think ring(0) works like that in this case.

          Comment

          Working...
          X