Announcement

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

  • grc1leg: Still not getting the subgraph sizes right...

    Hello everybody,

    I guess it's a classic and if you can point me to the appropriate earlier thread, I would be very grateful.
    Anyway, I have been struggling with this since I first tried to combine graphs in Stata and I have tried everything I could think of, but I still don't get it.

    My problem:
    I would like to combine three subgraphs in one rows (in this case using Vince Wiggins' grc1leg but it's all the same with graph combine).
    I would like the sub graphs to keep an aspect ratio of about 1, i.e. similar x and y size, i.e. about a square shape.
    But by default they become rather narrow and tall.

    Below you can see some of the various attempts I took trying to follow help graph combine ("Controlling the aspect ratio of subgraphs").
    But I could never get to a satisfying result.
    The best I could do was attempt B, but I could never get rid of the margin around the subgraphs.

    The solution I found was attempt C using post-processing with gr_edit to change the overall x and y length of the combined graph.
    But there must be some way to achieve this directly, right?
    (Btw: adding xsize(5) ysize(2) as options to grc1leg or graph combine doesn't do the trick).

    So, please help me to solve this once and for all!
    Thank you in advance and best
    Boris


    Code:
    sysuse auto, clear
    
    // findit grc1leg // By Vince Wiggins
    
    * A Not good:
        twoway (scatter price mpg)(scatter gear_ratio turn), name(gr1,replace)
        twoway (scatter weight trunk), name(gr2,replace)
        twoway (histogram headroom), name(gr3,replace)
        
        grc1leg  gr1 gr2 gr3, rows(1) name(not_good,replace)
            // sub graphs too narrow and tall
        
        
    * B Slightly better:
        twoway (scatter price mpg)(scatter gear_ratio turn), legend(order(1 "Price")) fysize(50)  name(gr1,replace)
        twoway (scatter weight trunk), fysize(50) name(gr2,replace)
        twoway (histogram headroom), fysize(50) name(gr3,replace)
        
        grc1leg  gr1 gr2 gr3, rows(1) imargin(0 0 0 0) name(slightly_better,replace)
            // Now the subgraphs look nice, but there is a huge margin between them and the surrounding graph
    
    * C That's it:
        twoway (scatter price mpg)(scatter gear_ratio turn), name(gr1,replace)
        twoway (scatter weight trunk), name(gr2,replace)
        twoway (histogram headroom), name(gr3,replace)
        
        grc1leg  gr1 gr2 gr3, rows(1) iscale(1) name(thats_it,replace)
        
        gr_edit .style.editstyle declared_ysize(2) editcopy
        gr_edit .style.editstyle declared_xsize(5) editcopy

  • #2
    The ysize() and xsize() options seem to achieve the intended effect when used with graph combine:

    Code:
    twoway (scatter price mpg)(scatter gear_ratio turn), name(gr1,replace)  
    twoway (scatter weight trunk), name(gr2,replace)  
    twoway (histogram headroom), name(gr3,replace)
    
    gr combine gr1 gr2 gr3, rows(1) iscale(1) ysize(2) xsize(5) imargins(0 0 0 0) name(grc,replace)
    Click image for larger version

Name:	graph combine.png
Views:	1
Size:	83.9 KB
ID:	1617211




    However, the exact same options, when applied to grc1leg, produce a graph like this:
    Click image for larger version

Name:	grc1leg.png
Views:	1
Size:	92.8 KB
ID:	1617212




    A quick look at the source for grc1leg reveals this line, which I suspect is the problem:

    Code:
    gr draw `name'
    When grc1leg redraws the graph with the new legend applied, it does not retain the ysize and xsize options specified in the original command. This can be solved by redrawing the graph yourself after running grc1leg:

    Code:
    grc1leg gr1 gr2 gr3, rows(1) iscale(1)  imargins(0 0 0 0) name(grc1leg,replace)
    gr draw grc1leg,  ysize(2) xsize(5)
    Which produces what I think you want:
    Click image for larger version

Name:	grc1leg2.png
Views:	1
Size:	50.6 KB
ID:	1617213

    Last edited by Ali Atia; 01 Jul 2021, 15:06.

    Comment


    • #3
      Dear Ali,

      thank you for your answer. I should have tried it with graph combine first before claiming it doesn't work there either.
      So I guess post-processing or redrawing is indeed the easiest solution with grc1leg, whereby redrawing is probably faster when used on many graphs.
      Thank you clarifying!

      Best regards
      Boris

      Comment

      Working...
      X