[R] multiple scale

Jeanie (Jie) Na naj at ccf.org
Tue Aug 2 19:10:04 CEST 2005


Hi, 
Search the list, you might be able to find the message but here it is.

Copied From Dr. Brian Ripley's post
> x <- 1:10
> y <- rnorm(10)
> z <- runif(10, 1000, 10000)
> par(mar=c(5,4,4,4) + 0.1)  # Leave space for z axis
> plot(x, y)
> par(new=T)
> plot(x, z, type="l", axes=F, bty="n", xlab="", ylab="")
> axis(4, at=pretty(range(z)))
> mtext(z, 4, 3)
change last line to mtext("z", 4, 3).

J.
On Tue, 2005-08-02 at 17:18 +0100, herodote at oreka.com wrote:
> Hi all
> 
> i need to put on one graph 2 functions who's x axis is the same and y not.
> 
> I mean on horizontal the time, and on vertical left: pressure, on vertical right: rpm of a motor, is R able to do that?
> 
> i've found this that i could adapt maybe (i don't need time series really?) :/ :
> 
> (http://tolstoy.newcastle.edu.au/R/help/04/03/1456.html)
> ## 
> ## Description: A simple function which plots two time series on one plot where 
> ## the series can have different value intervals over the same time interval. 
> ## Usage: ts.plot.2Axis(xleft, xright) 
> ## Arguments: xleft is the time series for the left vertical axis and xright 
> ## is for the right axis. xleft and xright are defined as time series with 
> ## the 'ts' function in package ts. 
> ## ts.plot function must be available, do library(ts) to ensure this if 
> ## necessary. 
> ## In addition the usual 'ts.plot' and 'plot' parameters can be set 
> ## directly (mar, main, xlab, ylab, lwd) or through gpars as in ts.plot. 
> ## Also parameter digits is the preferred number of decimal digits on right 
> ## axis and ticks is the preferred number of tick marks on right axis. 
> ## Details: The time series for the right vertical axis is scaled with a simple 
> ## rule of thumb scaling. 
> ## The ts.plot function is used to plot the series. 
> ## Value: None. 
> ## Note: When scaling is not acceptable try switching the series parameters. 
> ## If a ylabel is to be set it is here only possible for the left axis. 
> ## See also: 'ts.plot', 'ts', 'legend'. 
> ## Author and date: Hauksson, Bjorn Arnar. March 2004. 
> ## Example: 
> ## First paste this function into the R console or use 'source'. 
> #library(ts) 
> #data(UKLungDeaths) 
> #x <- ldeaths 
> #y <- fdeaths/mdeaths 
> #ts.plot.2Axis(x, y) 
> #legTxt <- c("UK lung deaths", "UK female/male deaths (rhs)") 
> #legend(1976.5, 3950, legTxt, lty=c(1:2), col=c(1:2), lwd=2, bty="n") 
> ## 
> 
> 
> ts.plot.2Axis <- function(xleft, xright, digits=1, ticks=5, 
>                           mar=(c(4,4,4,4)+0.1), main="", 
>                           xlab="", ylab="", lwd=2, gpars=list()) { 
>         # Settings for other parameters than those in the function parameter list 
>         par(mar=mar) # Margins 
>       k <- ncol(as.matrix(xleft)) # Number of time series on left vertical scale 
>         lty <- c(1:(k+1)) # Line types 
>         col <- c(1:(k+1)) # Line colors 
> 
> 
>         # Scale time series on right vertical axis 
>         scale <- (max(xleft)-min(xleft))/(max(xright)-min(xright)) 
>       xright2 <- xright*scale 
>       meanScale <- mean(xleft) - mean(xright2) 
>         xright2 <- xright2 + meanScale 
> 
> 
>         # Plot the series 
>         ts.plot(xleft, xright2, lty=lty, col=col, main=main, ylab=ylab, xlab=xlab, 
>         lwd=lwd, gpars=gpars) 
> 
> 
>         # Add the right vertical axis labels 
>         lab <- seq(round(min(xright), digits), round(max(xright), digits), 
>         length=ticks) 
>         labAt <- seq(min(xright2), max(xright2), length=ticks) 
>         axis(side=4, labels=lab, at=labAt) 
> } 
> 
> are there a better way to plot what i want?
> 
> thks.
> guillaume.
> 
> ////////////////////////////////////////////////////////////
> // Webmail Oreka : http://www.oreka.com
> ////////////////////////////////////////////////////////////
> 
> ______________________________________________
> 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
> 
-- 
Jeanie (Jie) Na

Programmer Analyst II                       _   _    ___    _   _    ____
Department of Quantitative Health Sciences [_]-[_]  / - \  | |_| |  (_(_
Cleveland Clinic Foundation                 |   |  ( |_| ) )  _  (   _| )
Tel: (216)4451369                          [_]-[_]  \_\_\  |_| |_|  (___/




More information about the R-help mailing list