In this post I give a quick tip on how to embed one chart within another chart. This can be useful in situations where you want to expand a part of your chart or show distinct ranges of your data set.
Trend analysis with Excel is the most popular page on my ProcessTrends website. My Excel regression workbooks are some of my top downloads, with over 1,800 downloads so far this year. When I apply regression to a subset of the data, I’d like to show both the overall data set with the regression and a blow-up of the subset with the regression line.
R lets you add a 2nd chart within the 1st chart by adjusting the par(fig()) setting. Let’s look at an example, then I show how I did it.
This chart shows the recent trend (1975 to 2008) and a small insert chart that shows the full 1880-2008 data set with the same regression line. This dual chart display lets the user see the selected regression period in large scale and also see what portion of the full data set is covered by the selected regression period. This is a chart within a chart.
How To Embed a Chart Within a Chart
To make an embedded chart, we adjust the figure dimension for the insert chart with respect to the main chart with this statement:
par(fig=c(x_left, x_right, y_bottom, y_top), new = T)
x_left, x_right2, y_bottom, y_top are portions of the main chart X and Y spans.
Pretty simple. Just make both charts and set par(fig=)) just before making the embedded chart. It’s a good idea to return the par(fig=c(0,1,0,1) at the end of script to avoid any carryover of parameter adjustments to your next plot in the same R session.