Last data update: 2014.03.03

R: Plot with two y axes
y2plotR Documentation

Plot with two y axes

Description

Function to plot two y variables, y1 and y2, against a single x variable, with the y1 and y2 axes on the left and right of the plot respectively.

Usage

y2plot(x, y1, y2 = NULL, labels, y2par = list(), add = FALSE,
  xy = NULL, xlegend = "topleft", inset = 0.04, ...)

Arguments

x

vector of ages.

y1

vector of measurements for plotting on left y axis.

y2

optional vector of measurements for plotting on right y axis.

labels

character vector containing labels for x, y1 and y2.

y2par

optional named list of par arguments to format the y2 axis.

add

logical flag to specify if a new plot (with axes etc) is to be drawn (FALSE) or an existing plot is to be added to (TRUE).

xy

optional list to pass usr and usr2 (see Value).

xlegend

position for legend.

inset

inset for legend.

...

optional par arguments.

Details

y2plot draws up to two superimposed plots, one with the y axis on the left and the other on the right, with suitable adjustment for par('mar') and including a legend. The format for y1 is controlled by par arguments, and that for y2 by the list y2par.

Value

Returns the list

usr

par('usr') for y1 axis

usr2

par('usr') for y2 axis

In addition the variable .par.usr2, equal to usr2, is created in globalenv().

Author(s)

Tim Cole tim.cole@ucl.ac.uk

See Also

plot.sitar

Examples

## plot boys median height and weight on the UK 1990 reference
with(uk90[uk90$sex == 1,],
  y2plot(x=years, y1=M.ht, y2=M.wt, y2par=list(col='red'))
)

Results