[R] multiple scale

herodote@oreka.com herodote at oreka.com
Tue Aug 2 18:18:15 CEST 2005


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
////////////////////////////////////////////////////////////




More information about the R-help mailing list