Overlaying & combining graphs

Sometimes you might want to depict different statistics and graph types in one graph. You can:

  • depict several graphs on one axis by overlaying them
  • combine two or more graphs next to each other

Some hints:

  • If you want to combine two or more graphs with the same legend, use the user-written command grc1leg to only display the legend once in the combined graph (ssc install grc1leg before you use it for the first time).
  • If you want to combine two or more graphs which should have different sizes, use the options fxsize() and fysize() when constructing the single graphs. They specify how many percent of the available height (fysize) respectively width (fxsize) should be occupied by the single graph. Your single graphs will look strange, but the combined graph will look nice. You find an example in the help-file for "graph combine" (>>Remarks>>Controlling the aspect ratio of subgraphs)

* Overlaying plots
sysuse auto, clear

// Start with simple scatter plots: each is displayed one after the other, separately 
scatter price weight 
scatter price weight if foreign==0
scatter price weight if foreign==1

// Plots can be overlaid by putting each plot into ()
// two scatter plots
twoway (scatter price weight if foreign==0) (scatter price weight if foreign==1)

// one scatter plot and one line plot
sort weight
twoway (scatter price weight if foreign==0) (line price weight if foreign==1)
// need to sort by weight first due to line plot

// Add options to the overall graph 
twoway (scatter price weight if foreign==0) (scatter price weight if foreign==1), ///
legend(label(1 "Domestic cars") label(2 "Foreign cars")) ///
title("Raw correlations of price and weight by foreign status")

// Change the look of one sub-graph by adding options inside the respective () 
twoway (scatter price weight if foreign==0, msymbol(x)) (scatter price weight if foreign==1), ///
legend(label(1 "Domestic cars") label(2 "Foreign cars")) ///
title("Raw correlations of price and weight by foreign status")
			
* Combining graphs
sysuse auto, clear

// Graphs can be combined by assigning each graph a name and using the command graph combine

// First graph is named "g_domestic"
hist price if foreign==0, fraction width(600) start(500) title("Relative frequencies of price") ///
name(g_domestic, replace)
// Second graph is named "g_foreign"
hist price if foreign==1, fraction width(600) start(500) title("Relative frequencies of price") ///
name(g_foreign, replace) yscale(off)
// Graph combine displays both graph next to each other
graph combine g_domestic g_foreign
// Specify in how many columns or rows the graphs are displayed
graph combine g_domestic g_foreign, rows(2) cols(1)
// Give y and x axes common scales
graph combine g_domestic g_foreign, ycommon xcommon