Announcement

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

  • Using graph combine with different legends

    Hello,

    I'm trying to combine two twoway plots with the same y axis. However, each plot has a different number of series on it and so the legends are different lengths. When I try to combine the two plots, the legends make it so that the graphs are scaled differently. Also, for readability, it's pretty important that the legends all be one column.
    Code:
    sysuse auto, clear
    twoway scatter  mpg rep78 headroom trunk price, name(fig1, replace)
    twoway scatter weight turn price, name(fig2, replace)
    graph combine fig1 fig2, xcommon ycommon
    Click image for larger version

Name:	ex1.JPG
Views:	1
Size:	62.2 KB
ID:	1430037


    It looks a bit better if I fix the aspect ratio, but the graphs are still not lined up with each other.

    Code:
    twoway scatter  mpg rep78 headroom trunk price, aspectratio(1) name(fig1, replace)
    twoway scatter gear_ratio turn price, aspectratio(1)  name(fig2, replace)
    graph combine fig1 fig2, xcommon ycommon
    Click image for larger version

Name:	ex2.JPG
Views:	1
Size:	59.2 KB
ID:	1430038


    (Ignore the fact that these graphs are completely meaningless; they illustrate my issue).

    I'm looking for something like the last graph here, though this is coefplot not twoway.

    Thanks for your help!
    Last edited by Brina Seidel; 14 Feb 2018, 10:01.

  • #2
    This is an issue with the size of the legends in the individual graphs. For -twoway-, the legend is part of the graph, so you need to make sure that the vertical space occupied by the legends (i.e. no. of rows) matches in the two graphs prior to combining them. Based on your example:

    Code:
    sysuse auto, clear
    twoway scatter  mpg rep78 headroom trunk price, name(fig1, replace)
    twoway scatter weight turn price, legend(rows(2)) name(fig2, replace)
    graph combine fig1 fig2, xcommon ycommon
    Click image for larger version

Name:	gclegend.png
Views:	1
Size:	74.2 KB
ID:	1430148


    Other ideas include shifting the position of the legend to within the graph if it does not obscure any points.

    Comment


    • #3
      Hi Andrew,

      Thanks so much for your reply. As I mentioned, it's pretty important for readability reasons that my legends have one column. (My real data has a couple similar series with slight differences in methodology, so 1) the labels are very long and 2) stacking them on top of each other makes the differences much easier for the reader to see. These graphs are probably going to be published exactly as they come out of Stata so I have to keep these things in mind.)

      From your post, am I to understand that there's no way to fix the position of the plot region and the legend separately in a twoway plot? If so, that's helpful to know but quite unfortunate :/

      I'll play around with putting the labels on the graph itself but I'm not sure if there's space.

      Thanks again for your time.

      --Brina


      Comment


      • #4
        From your post, am I to understand that there's no way to fix the position of the plot region and the legend separately in a twoway plot?
        This is as far as I know given that the legend is part of the graph. If you are willing to trade off the plot region to accommodate a long legend, then there are options to effect this. You can play around but I estimate that for each row, you need double the number of holes in the legend.

        Code:
        sysuse auto, clear
        twoway scatter mpg rep78 headroom trunk price, legend(rows(4)) name(fig1, replace)
        twoway scatter weight turn price, legend(rows(8) holes(1/3, 6/8)) name(fig2, replace)
        graph combine fig1 fig2, xcommon ycommon
        For the second graph, I specify 8 rows for the legend (twice that of the 1st) and place the markers in positions 4 and 5. Positions 1-3 and 6-8 are occupied by holes. This makes the plot regions approximately equal.
        Click image for larger version

Name:	grcomb.png
Views:	1
Size:	76.8 KB
ID:	1430186

        Comment


        • #5
          This is perfect -- my scheme gets rid of all the legend borders etc. so it looks really nice. Thanks so much for your help!

          Comment

          Working...
          X