[R] Second y-axis --- alternative to par(new=

jim holtman jholtman at gmail.com
Thu Oct 8 14:30:04 CEST 2009


What you need to do is to specify that you don't want labels on the second plot:

## (2) Does plot points where (x-axis) they should be
xlim <- range(x1)
plot(x1, y1, type="l", xlim=xlim)
par(new=TRUE)
plot(x2, y2, type="p", xlim=xlim, xlab='', ylab='', axes=FALSE)
axis(4)

On Wed, Oct 7, 2009 at 4:29 AM, Rainer M Krug <r.m.krug at gmail.com> wrote:
> Hi
>
> is there an alternative to par(new), for ading data to a plot for a
> different y-axis?
> My problem with par(new=TRUE) is, that it re-defines all axis and labels (as
> in example 1) and one has to use xlim=... to fix the x-axis.
> I am looking for something, which simply resets the y-axis, so that a new
> plot() (or points()/lines()) keeps the x-axis, but re-defines the y-axis.
>
> Is there something available? I could re-scale the y-data to fit the
> existing y-axis, but that would mean to define all the labels and tickmarks
> for axis 4 manually.
>
> HEre is an example about what I mean:
>
> x1 <- 1:10
> y1 <- runif(10)
> x2 <- 1:11
> y2 <- c(y1*100, 0)
>
> ## (1) Does not plot points where (x-axis) they should be
> plot(x1, y1, type="l")
> par(new=TRUE)
> plot(x2, y2, type="p")
> axis(4)
>
> ## (2) Does plot points where (x-axis) they should be
> xlim <- range(x1)
> plot(x1, y1, type="l", xlim=xlim)
> par(new=TRUE)
> plot(x2, y2, type="p", xlim=xlim)
> axis(4)
>
> Cheers,
>
>
> Rainer
> --
> Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation Biology,
> UCT), Dipl. Phys. (Germany)
>
> Centre of Excellence for Invasion Biology
> Natural Sciences Building
> Office Suite 2039
> Stellenbosch University
> Main Campus, Merriman Avenue
> Stellenbosch
> South Africa
>
> Cell:           +27 - (0)83 9479 042
> Fax:            +27 - (0)86 516 2782
> Fax:            +49 - (0)721 151 334 888
> email:          Rainer at krugs.de
>
> Skype:          RMkrug
> Google:         R.M.Krug at gmail.com
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?




More information about the R-help mailing list