Announcement

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

  • graph combine distorts aspect ratio when combining fewer graphs

    Cross posted from here because I initially posted in the wrong sub-forum by mistake: https://www.statalist.org/forums/for...g-fewer-graphs

    Hello,

    I'm encountering an issue when using the graph combine command to arrange a set of graphs in a grid layout. Specifically, I want to display 10 graphs in a 2x2 layout for visual balance. This is done by producing 3 combined graphs: the first 2 have 4 graphs each and the third has the remaining 2 graphs. However, since the final row contains just two graphs (i.e., a 2x1 grid), Stata stretches the height of these graphs to fill the space, distorting the aspect ratio.

    Below is a reproducible example that illustrates the problem:

    Code:
    clear all
    sysuse auto, clear
    
    cd "" // replace with a folder path
    
    gen obs_order = _n
    xtile mygroup = obs_order, n(6)
    
    foreach yy of numlist 1/6 {
        graph bar price foreign if mygroup == `yy'
            graph save graph_`yy', replace    
    }
    
    // This 2x2 layout looks fine – proportions are preserved
    graph combine graph_1.gph graph_2.gph graph_3.gph graph_4.gph, col(2) row(2)
    
    // This 2x1 layout is distorted – vertical stretching occurs
    graph combine graph_5.gph graph_6.gph, col(2) row(2)
    In the first combined graph (4 graphs in a 2x2 layout), the aspect ratio is preserved as expected. However, in the second combined graph (2 graphs), Stata stretches the graphs vertically, presumably trying to fill space for non-existent graphs 7 and 8.

    Is there a way to prevent Stata from stretching the graphs when combining fewer graphs? Ideally, I’d like to keep the same aspect ratio or dimensions as in the earlier graph combine.

    Thank you in advance for your help!

    Best,
    Hélder

  • #2
    I'm not sure I understand what you're asking. You refer to 10 graphs, but your example only has 6. And I don't grasp the point of combining the 10 graphs (assuming you really mean 10) into groups of 4, 4, and 2. Why don't you just do:
    Code:
    clear all
    sysuse auto, clear
    
    
    gen obs_order = _n
    xtile mygroup = obs_order, n(10)
    
    foreach yy of numlist 1/10 {
        graph bar price foreign if mygroup == `yy', name(graph_`yy', replace)
    }
    
    graph combine graph_1 graph_2 graph_3 graph_4 graph_5 graph_6 graph_7 ///
        graph_8 graph_9 graph_10, rows(3) cols(4) holes(9 12) nocopies
    Notes: I'm working with the graphs in memory rather than saving them to disks, but that will not affect the issues presently in focus. Also, the -nocopies- option has no effect on the layout of the graphs--I'm just using it because it makes -graph combine- run (much) faster.

    If the above is satisfactory except for the ordering of the graphs, then you can get the order that combining the combinations would be by simply adjusting the order of the graphs mentioned in -graph combine-:
    Code:
    clear all
    sysuse auto, clear
    
    
    gen obs_order = _n
    xtile mygroup = obs_order, n(10)
    
    foreach yy of numlist 1/10 {
        graph bar price foreign if mygroup == `yy', name(graph_`yy', replace) ///
            title(Graph `yy')
    }
    
    graph combine graph_1 graph_2 graph_5 graph_6 graph_3 graph_4 graph_7 ///
        graph_8 graph_9 graph_10, rows(3) cols(4) holes(9 12) nocopies

    Comment


    • #3
      Thank you for you reply and feedback on this, Clyde!

      My example only had 6 because that's the number I needed to show you a graph that combines 4 graphs and another with only 2 combined graphs, so that a comparison could be seen of how to looks with 4 vs 2 - and that you could see that when only including 2, they get stretched.

      I want to group them into 4, 4 and 2, because I want include them in Microsoft Word document, and I need them to be clearly readable. Having them in a 2x2 grid also maintains their original aspect ratio, which I think makes it aesthetically pleasing. Furthermore, separating them into different graphs (instead of having a graph with 2 columns and 5 rows) allows me to continue the graph in the next page, if needed.

      Therefore, I'm looking for a solution that doesn't stretch a combined graph if I only have two.

      10 is the number of countries I have at the moment, if I get 11 or 12, there will be something to include in the second row of the third combined graph, and the problem goes away. But I might actually get data at a later date for 13 or 14 (thus making a fourth combined graph), which makes the problem come back. It also makes the problem of having the graphs clearly readable more challenging if I were to follow your suggested approach, as with 10 the graphs are already quite small in my opinion.

      I hope this made it clearer what is the issue and the desired outcome.

      Comment


      • #4
        You might get closer to what you want with this:

        Code:
        clear all
        sysuse auto, clear
        
        gen obs_order = _n
        xtile mygroup = obs_order, n(6)
        
        foreach yy of numlist 1/6 {
            graph bar price foreign if mygroup == `yy', yscale(range(8000)) name(graph_`yy', replace)    
        }
        
        // This 2x2 layout looks fine – proportions are preserved
        graph combine graph_1 graph_2 graph_3 graph_4, col(2) row(2) name(grc_4, replace) ycommon nocopies ysize(4) altshrink
        
        // This 2x1 layout is distorted – vertical stretching occurs
        graph combine graph_5 graph_6, col(2) row(2) name(grc_2, replace) ycommon nocopies ysize(2) altshrink

        Comment


        • #5
          Thank you Hemanshu Kumar, this seems to fix the issue!

          Comment

          Working...
          X