Last data update: 2014.03.03

R: Embed a new plot within an existing plot
subplotR Documentation

Embed a new plot within an existing plot

Description

Subplot will embed a new plot within an existing plot at the coordinates specified (in user units of the existing plot).

Usage

subplot(fun, x, y, size=c(1,1), vadj=0.5, hadj=0.5, pars=NULL)

Arguments

fun

an expression or function defining the new plot to be embedded.

x

x-coordinate(s) of the new plot (in user coordinates of the existing plot).

y

y-coordinate(s) of the new plot, x and y can be specified in any of the ways understood by xy.coords.

size

The size of the embedded plot in inches if x and y have length 1.

vadj

vertical adjustment of the plot when y is a scalar, the default is to center vertically, 0 means place the bottom of the plot at y, 1 places the top of the plot at y.

hadj

horizontal adjustment of the plot when x is a scalar, the default is to center horizontally, 0 means place the left edge of the plot at x, and 1 means place the right edge of the plot at x.

pars

a list of parameters to be passed to par before running fun.

Details

The coordinates x and y can be scalars or vectors of length 2. If vectors of length 2 then they determine the opposite corners of the rectangle for the embedded plot (and the parameters size, vadj, and hadj are all ignored.

If x and y are given as scalars then the plot position relative to the point and the size of the plot will be determined by the arguments size, vadj, and hadj. The default is to center a 1 inch by 1 inch plot at x,y. Setting vadj and hadj to (0,0) will position the lower left corner of the plot at (x,y).

The rectangle defined by x, y, size, vadj, and hadj will be used as the plotting area of the new plot. Any tick marks, axis labels, main and sub titles will be outside of this rectangle.

Any graphical parameter settings that you would like to be in place before fun is evaluated can be specified in the pars argument (warning: specifying layout parameters here (plt, mfrow, etc.) may cause unexpected results).

After the function completes the graphical parameters will have been reset to what they were before calling the function (so you can continue to augment the original plot).

Value

An invisible list with the graphical parameters that were in effect when the subplot was created. Passing this list to par will enable you to augment the embedded plot.

Author(s)

Greg Snow greg.snow@imail.org

See Also

cnvrt.coords, par, symbols

Examples

# make an original plot
plot( 11:20, sample(51:60) )

# add some histograms

subplot( hist(rnorm(100)), 15, 55)
subplot( hist(runif(100),main='',xlab='',ylab=''), 11, 51, hadj=0, vadj=0)
subplot( hist(rexp(100, 1/3)), 20, 60, hadj=1, vadj=1, size=c(0.5,2) )
subplot( hist(rt(100,3)), c(12,16), c(57,59), pars=list(lwd=3,ask=FALSE) )

tmp <- rnorm(25)
qqnorm(tmp)
qqline(tmp)
tmp2 <- subplot( hist(tmp,xlab='',ylab='',main=''), 
		cnvrt.coords(0.1,0.9,'plt')$usr, vadj=1, hadj=0 )
abline(v=0, col='red') # wrong way to add a reference line to histogram

# right way to add a reference line to histogram
op <- par(no.readonly=TRUE)
par(tmp2)
abline(v=0, col='green')
par(op)


Results


R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(Hmisc)
Loading required package: lattice
Loading required package: survival
Loading required package: Formula
Loading required package: ggplot2

Attaching package: 'Hmisc'

The following objects are masked from 'package:base':

    format.pval, round.POSIXt, trunc.POSIXt, units

> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/Hmisc/subplot.Rd_%03d_medium.png", width=480, height=480)
> ### Name: subplot
> ### Title: Embed a new plot within an existing plot
> ### Aliases: subplot
> ### Keywords: aplot dplot
> 
> ### ** Examples
> 
> # make an original plot
> plot( 11:20, sample(51:60) )
> 
> # add some histograms
> 
> subplot( hist(rnorm(100)), 15, 55)
> subplot( hist(runif(100),main='',xlab='',ylab=''), 11, 51, hadj=0, vadj=0)
> subplot( hist(rexp(100, 1/3)), 20, 60, hadj=1, vadj=1, size=c(0.5,2) )
> subplot( hist(rt(100,3)), c(12,16), c(57,59), pars=list(lwd=3,ask=FALSE) )
> 
> tmp <- rnorm(25)
> qqnorm(tmp)
> qqline(tmp)
> tmp2 <- subplot( hist(tmp,xlab='',ylab='',main=''), 
+ 		cnvrt.coords(0.1,0.9,'plt')$usr, vadj=1, hadj=0 )
> abline(v=0, col='red') # wrong way to add a reference line to histogram
> 
> # right way to add a reference line to histogram
> op <- par(no.readonly=TRUE)
> par(tmp2)
> abline(v=0, col='green')
> par(op)
> 
> 
> 
> 
> 
> 
> 
> dev.off()
null device 
          1 
>