Here are two ways:
## method 1
plot(1:100,y1)
par(new=TRUE)
plot(1:100,y2,xlab="",ylab="",col=2,axes=FALSE)
axis(4,col=2,col.axis=2)
## method 2
plot.new()
plot.window(xlim=c(1,100),ylim=range(y1))
points(1:100,y1)
axis(1)
axis(2)
title(xlab="x",ylab="y1")
plot.window(xlim=c(1,100),ylim=range(y2))
points(1:100,y2)
axis(4,col=2,col.axis=2)
box()
--- Young Cho <young.stat at gmail.com> wrote:
> When I try to plot two lines ( or scatterplots) with different scales, this
> is what I have been doing:
>
> Suppose: I have y1 and y2 in a very different scale
>
> y1 = 1:100
> y2 = c(100:1)*10
>
> To plot them on top of each other and denote by different colors: I have
> to
> figure out the correct scale '10' and corresponding tick.vector
and
> lables.
> Then do:
>
> plot(1:100, y1) # I can have 'ylab' here for the left-hand side y
axis.
> points(1:100, y2/10,col=2)
> ytick.vector = seq(from=0,to=100,by=20)
> ytick.label = as.character(seq(from=0,to=1000,by=200))
> axis(4,at = ytick.vector,label = ytick.label,col=2,col.axis=2)
>
> Two questions.
>
> 1. Are there easier ways to plot the y1, y2 w/o figuring out the correct
> scaler, tick vectors, and labels in order to put them in one figure?
> 2. How to add additional 'ylab' to the right hand side y-axis of
the plot?
> Thanks a lot!
>
> -Young
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>