Announcement

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

  • twoway: Align y-axis lines of subplots in "graph combine" when y-axis label have different length

    Hello everybody,

    I am creating two subplots that I would like to combine with "graph combine". The lengnth of the y-axis labels differs and, as a consequence, the space between the y-axis line and the margin of the plot differs, which does not look very nice when combine the two plots.
    Here's a code example and the output:

    Code:
    sysuse auto, clear
    
    capt ssc install cleanplots
    set scheme cleanplots
    
    twoway (scatter price weight, graphregion(lcol(black)) name(g1, replace))
    twoway (scatter mpg weight, graphregion(lcol(black)) name(g2, replace))
    
    graph combine g1 g2, xcommon rows(2)
    Click image for larger version

Name:	plot_exmpl.png
Views:	1
Size:	36.6 KB
ID:	1770881





    The problem is the offset between the y-axes (see red rectangle). I thought a solution could be to explicitly control the space between the y-axis line and the graph margin (see green arrows), but I could not figure out how to do that.
    Does anybody have a suggestion how to solve this issue?

    By the way: I would like the y-labels to be horizontal and to combine the subplots in 2 rows -- so simply changing these design aspects is not a solution

    I tried searching the forum for related posts in advance, but could'nt find any -- but if you know of one, please share!
    Thanks in advance!

    Best,
    Boris
    Last edited by Boris Ivanov; 16 Jan 2025, 06:30.

  • #2
    Short answer is that graph combine isn't smart enough to read your mind on this.

    There are many posts in this territory, but I do agree that they are hard to find as the usual keywords yield too many hits.

    In essence, the key here is to recast the problem so that internally your graphs are coming from a by() option.

    The most systematic discussion I can recall is at https://journals.sagepub.com/doi/pdf...36867X20976341

    You don't have to write code to that effect as multiline from SSC will do most of the work for you.

    https://www.statalist.org/forums/for...ailable-on-ssc

    Code:
    sysuse auto, clear
    
    multiline price mpg weight, recast(scatter) c(none)
    Click image for larger version

Name:	multiline.png
Views:	1
Size:	49.0 KB
ID:	1770883


    I am presuming that the graph scheme used here is secondary. I used stcolor.

    Comment


    • #3
      Dear Nick,
      thank you for the quick and very useful reply.
      That's what I was looking for!

      Best,
      Boris

      Comment

      Working...
      X