waanders
waanders

Reputation: 9083

Multiple axis/scales on a XY-plot in R

How can I plot a second line in a XY-plot, using plot(), to a different scale like this example (purple line)?

enter image description here

My R code for the first (red) line is something like:

p <- sqlQuery(ch,"SELECT wl,param1 FROM qryPlot ORDER BY wl")
plot(p$wl,p$param1,axes=T,xlim=c(400,800),ylim=c(0,100),type="l",col="red")

Upvotes: 2

Views: 4437

Answers (2)

waanders
waanders

Reputation: 9083

I slightly extended the answer by @johncolby to this:

x<-1:20
y1<-sqrt(x)
y2<-sqrt(x)*x
plot(x,y1,ylim=c(0,25),col="blue")
par(new=TRUE)
plot(x,y2,ylim=c(0,100),col="red",axes=FALSE)
axis(4)

(axes=FALSE in second plot() command = to prevent labels second axis printed on the left side)

With this result:

enter image description here

Little problem to solve: labels both y-axes are printed to the left side.

Upvotes: 1

John Colby
John Colby

Reputation: 22588

Here is the general idea:

plot(1:10)
par(new=T)
plot(1:10, rep(50, 10), type='l', axes=F, xlab=NA, ylab=NA)
axis(4)

enter image description here

Upvotes: 9

Related Questions