# R Lets You Put Chart Inside Chart

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.

Here is the plot inside plot R script;

```
## plot Inside plot example #####################################################
library(plyr)
url <- c("http://data.giss.nasa.gov/gistemp/graphs_v3/Fig.A2.txt")
test<- c("C://R_Home//Charts & Graphs Blog//RClimateTools//a_Revised_Blog//test")
## Cleanup File
#The first 4 rows and last row contain text strings
# Read file as  char vector, one line per row, Exclude first 7 rows
textConnection(lines), header=F, colClasses = "character",na.strings = c("*"))
closeAllConnections()
# We are only interested in the year and annual data in first 2 columns
df <- df[,1:2]
names(df)<- c("Year", "Anom")
# Convert variables (columns) to numeric format
df <- colwise(as.numeric) (df)
# Create 1st Plot full size
par(las=1, ps=12)
plot(df\$Year, df\$Anom, xlab="", ylab="Anomaly - C", xlim=c(1975,2013), type="b",
main="Plot Inside Plot: GISS Temperature Anomaly")
# Create Plot 2 as inset - adjust par(fig) to  desiredsize
#   x_left, x_right, y_bottom, y_top are portions of the main chart X and Y spans.
par(fig=c(0.5, 0.9, 0.1,0.525), new=T, las=1, ps=9)
plot(df\$Year, df\$Anom, xlab="", ylab="Anomaly - C", xlim=c(1880,2013), type="l",
main="Inset Plot: Long Term Trend")
# Return par(fig=) to full size for next plot
par(fig=c(0,1,0,1))

```

### 7 responses to “R Lets You Put Chart Inside Chart”

1. Rob

Could you at your actual R programming for the above plot? The sequencing is not clear to me.

• Kelly

Rob

I’ve added R script to show how I did it.

Kelly

2. Murksi

Thanks a lot!
I was looking for this all over the web, the only thing I found was complicated grid-based plotting…