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.
Introduction
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.
Quick Tip
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
I have shown how to make panel charts by adjusting the par(mfcol=c(#rows,# cols)) setting here, here, here and here.
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.



3 responses so far ↓
CO2 Emission Trends « Charts & Graphs with R // August 25, 2009 at 8:26 PM |
[...] CO2 levels are increasing. In previous posts, I have analyzed long term temperature trends (here, here, here, here) and compared CO2 – temperature trends (here). As I continue my climate change [...]
ggplot2: Plot Inside A Plot « Learning R // May 8, 2009 at 2:22 PM |
[...] Inside A Plot 2009 May 8 tags: chart, ggplot2, plot, R by learnr Charts & Graphs blog demonstrates how to embed one chart within another chart using base [...]
ggplot2: Graph Inside A Graph « Learning R // May 8, 2009 at 2:18 PM |
[...] Inside A Graph 2009 May 8 tags: chart, ggplot2, plot, R by learnr Charts & Graphs blog demonstrates how to embed one chart within another chart using base [...]