[R] plot with two y axes

DJ LUCY D.J.Lucy at Bradford.ac.uk
Mon Sep 11 11:08:33 CEST 2000


> On Thu, 7 Sep 2000, John P. Burkett wrote:
> 
> > I'm trying to make a transition from S-plus under Windows to R under
> > Linux.  My immediate aim is to produce a scatter plot with two y-axes
> > with different scales. That can be done in S-plus with a command of the
> > following form:
> > guiPlot( PlotType="Scatter",  DataSet="execon",
> > Columns="years,hstart,ship", AxisType="Multiple Y" ),
> > where years, hstart, and ship are columns in the dataset called execon.
> > Is there an equivalent command in R?   I'd be most grateful for any
> > suggestions.

John,

Last time I tried to do this I had to write a small function - it's not
perfect but it will provide some ideas. Watch out for the word wrapping on
my mailer.

The input is fairly self explanatory except:
noincs - no of divisions in the axis labels
marks - type of marker for each y
legpos - legend position
leglabs - legend labels

good luck.


Dave.

# plots an x y1 y2 using left and right axes for the different y's
# rescales y2 to fit in the same space as y1
TwoVarPlot <- function(xvar, y1var, y2var, labels, noincs = 5,
marks=c(1,2), legpos, leglabs, title)
{
# rescale to fit on same axis
scaledy2var <- (y2var - min(y2var)) / (max(y2var) - min(y2var))
scaledy2var <- (scaledy2var * (max(y1var) - min(y1var))) + min(y1var)

# plot it up and add the points
plot(xvar, y1var, xlab=labels[1], ylab="", axes=F, pch=marks[1],
main=title)
points(xvar, scaledy2var, pch=marks[2])

# make up some labels and positions
y1labs <- round(seq(min(y1var), max(y1var), length=noincs),2)

# convert these to the y2 axis scaling
y2labs <- (y1labs - min(y1var)) / (max(y1var) - min(y1var))
y2labs <- (y2labs * (max(y2var) - min(y2var))) + min(y2var)
y2labs <- round(y2labs, 2)

axis(1)
axis(2, at=y1labs, labels=y1labs)
axis(4, at=y1labs, labels=y2labs)
mtext(labels[3], side=4, line=2)
mtext(labels[2], side=2, line=2)
box()

legend(legpos[1], legpos[2], legend=leglabs, pch=marks, bty="n")
}








********************************************************************
Dr. David Lucy
Department of Archaeological Sciences
University of Bradford
Bradford
West Yorkshire
BD7 1DP
UK

tel. +44 01274 233556
fax. +44 01274 235190
********************************************************************

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list