Announcement

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

  • Align two graphs

    Here I have a simple example where I would like to alight a heatplot (ssc install heatplot) and a scatterplot. Is it possible that the y-axis data values are on the same level, the same goes for the title? Sadly, ycommon does not work here.


    Code:
    sysuse nlsw88, clear
    
    heatplot tenure wage ///
        , ramp(bottom format(%6.3f)) name(g1, replace) title("Test 1") aspectratio(1)
        
        
    twoway (scatter tenure wage) ///
        (lfit tenure wage) ///
        , name(g2, replace) title("Test 2") aspectratio(1) legend(pos(6))
        
    graph combine g1 g2, xcommon ycommon
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	225.0 KB
ID:	1730676
    Best wishes

    Stata 18.0 MP | ORCID | Google Scholar

  • #2
    Dear Felix,

    You are exploring the many options to control the twoway graph results as well as user contributed graph results. In my experience there are usually various alternatives to accomplish what you are looking for.
    Here I offer a rather simple (crude) solution by adjusting the graph region of the second plot:
    Code:
    sysuse nlsw88, clear
    
    heatplot tenure wage ///
        , ramp(bottom format(%6.3f)) name(g1, replace) title("Test 1") aspectratio(1) ysca(range(0 26))
        
    twoway (scatter tenure wage) ///
        (lfit tenure wage) ///
        , name(g2, replace) title("Test 2") aspectratio(1) legend(pos(6)) graphreg(m(t-1 b+4)) ysca(range(0 26))
    
    graph combine g1 g2, xcommon ycommon
    which results in:
    Click image for larger version

Name:	Combine_yaxis1.png
Views:	1
Size:	123.8 KB
ID:	1730694


    Note that besides the adjustment of the top and bottom marging by using
    Code:
    graphreg(m(t-1 b+4))
    I also adjust the scale of the yaxis of both graphs (plots) because without doing that the two axes differ a bit (actually they still do):
    Code:
    ysca(range(0 26))
    Below similar code but now I adjust the first graph to align the two:
    Code:
    sysuse nlsw88, clear
    
    heatplot tenure wage ///
        , ramp(bottom format(%6.3f)) name(g1, replace) title("Test 1") aspectratio(1) graphreg(m(b-4)) ysca(range(0 26))
    
    twoway (scatter tenure wage) ///
        (lfit tenure wage) ///
        , name(g2, replace) title("Test 2") aspectratio(1) legend(pos(6)) graphreg(m(t-1)) ysca(range(0 26))
        
    graph combine g1 g2, xcommon ycommon
    which results in:
    Click image for larger version

Name:	Combine_yaxis2.png
Views:	1
Size:	129.2 KB
ID:	1730695


    This just to show you that 'moving' the objects on (any) graph is functionality available in Stata that can be used to create basically anything that you can think of.
    But, I agree, it does take some effort to learn how to do that.
    http://publicationslist.org/eric.melse

    Comment


    • #3
      Thanks a lot Eric, this gives me the tools to adjust my graphs precisely!
      Best wishes

      Stata 18.0 MP | ORCID | Google Scholar

      Comment

      Working...
      X