[R] Plotting three time series on the same graph

Romain Francois rfrancois at mango-solutions.com
Mon Jun 30 13:33:08 CEST 2008


Hello Tolga,

Further hacking on par(mar) leads me to the code below. I hope it can 
help you.

Cheers,

Romain

y <- 1:100 + runif( 100, min = -10, max = 10 )
x <- 1000 + 1:100 + runif( 100, min = -10, max = 10 )
z <- seq( 70000, 25000, length.out = 100) + runif( 100, min = -2000, max 
= 2000)

.myLines <- function(x,y, col = "blue", lwd = 2, ...){
  lines( x,y,col = "black", lwd=2 + lwd)
  lines( x,y,col = col, lwd=lwd, ...)
}

transform <- function( data, n = 5, lim = range(data) ){   
  scaledData    <- ( data - lim[1] )  / ( lim[2] - lim[1] )
  tickMarks     <- pretty( data, n = n )
  tickPositions <- ( tickMarks - lim[1] )  / ( lim[2] - lim[1] )
  list( scaledData = scaledData, tickMarks = tickMarks, tickPositions = 
tickPositions )
}


par( mar = c(5,4,4,8)+.1 )
plot( 0, 0 , type = "n", las = 1, ylim = c(0,1), xlim = c(0,100), ann = 
F, axes=F )

x.tr <- transform(x)
y.tr <- transform(y)
z.tr <- transform(z)

# first draw the lines
.myLines( 1:100, x.tr$scaledData , col = "red" )
.myLines( 1:100, y.tr$scaledData , col = "blue" )
.myLines( 1:100, z.tr$scaledData , col = "green" )

axis(1)
box()

# then the axis
axis( 4, x.tr$tickPositions, x.tr$tickMarks, col = "red", col.axis = "red" )

par( mar = par("mar") - c(0,0,0,2.5) )
axis( 4, y.tr$tickPositions, y.tr$tickMarks, col = "blue", col.axis = 
"blue" )

par( mar = par("mar") - c(0,0,0,2.5) )
axis( 4, z.tr$tickPositions, z.tr$tickMarks, col = "green", col.axis = 
"green" )





tolga.i.uzuner at jpmorgan.com wrote:
>
> Thanks Romain for trying to help, but my issue is that I have three 
> time series, not two, so at least two of them would have to go on the 
> same side.
>
> There may be no solution to this problem in R but if there is, would 
> appreciate hearing about it from someone ?
>
> Figure 1 in this link shows what I am trying to achieve:
> http://www.aditus.nu/jpgraph/howto04.php
>
> Thanks anyway,
> Tolga
>
>
>
> *Romain Francois <rfrancois at mango-solutions.com>*
>
> 30/06/2008 10:51
>
> 	
> To
> 	tolga.i.uzuner at jpmorgan.com
> cc
> 	Matt Aldridge <maldridge at mango-solutions.com>
> Subject
> 	Re: [R] Plotting three time series on the same graph
>
>
>
> 	
>
>
>
>
>
> Hello Tolga,
>
> I am not sure what you are looking for is achievable easily, however,
> you can have an axis on the left and another on the right. Look at the
> side argument of the axis function. Would the following code work for you:
>
> # generating some data
> y <- 1:100 + runif( 100, min = -10, max = 10 )
> x <- 1:100 + runif( 100, min = -10, max = 10 )
> z <- seq( 70000, 25000, length.out = 100) + runif( 100, min = -2000, max
> = 2000)
>
> # prettier version of lines
> .myLines <- function(x,y, col = "blue", lwd = 2, ...){
>  lines( x,y,col = "black", lwd=2 + lwd)
>  lines( x,y,col = col, lwd=lwd, ...)
> }
>
> # transform from the scale c(25000,70000) to (0,100)
> transform <- function( data ){
>  ( data - 25000 )  / ( 70000 - 25000 ) * 100
> }
>
> par( mar = c(5,4,4,4)+.1 )
> plot( 1:100, x , type = "n", las = 1 )
> axs <- pretty( c(25000, 70000), n = 5 )
> abline( h = transform(axs), col = "gray")
> .myLines( 1:100, x, col = "red" )
> .myLines( 1:100, y, col = "blue" )
> .myLines( 1:100, transform(z), col = "green" )
> axis( 4, transform(axs), axs, las = 1 )
>
> Best Regards,
>
> Romain Francois
>
> -- 
> Mango Solutions
> data analysis that delivers
>
> Tel:  +44(0) 1249 76 77 00
> Fax:  +44(0) 1249 76 77 07
> Mob:  +44(0) 7813 52 61 23
>
> R for finance course: 8th-10th july. London
> The R Language: 9th-10th september. London
> More courses from: 
> http://www.mango-solutions.com/services/rtraining/r_schedule.html
>
>
>
> tolga.i.uzuner at jpmorgan.com wrote:
> > Dear R Users,
> >
> > I would like to plot three time series on the same graph, two axis 
> on the
> > left and one axis on the right.
> >
> > The time series that I am graphing on the left do not share a similar
> > scale: one has a range of 1:100 and the other a range of 
> 25000:70000. How
> > can I display the tick marks for both on the left hand side without
> > superimposing them, i.e. one set of tick marks in black right up on the
> > edge of the plot and another set of tick marks in red placed further to
> > the left so that the tick marks and axis identifiers do not sit on 
> top of
> > each other ?
> >
> > Many thanks in advance,
> > Tolga
> >  
>
>
> [attachment "plot3timeseries.png" deleted by Tolga I Uzuner/JPMCHASE]
> ------------------------------------------------------------------------
>
> Generally, this communication is for informational purposes only and 
> it is not intended as an offer or solicitation for the purchase or 
> sale of any financial instrument or as an official confirmation of any 
> transaction. In the event you are receiving the offering materials 
> attached below related to your interest in hedge funds or private 
> equity, this communication may be intended as an offer or solicitation 
> for the purchase or sale of such fund(s). All market prices, data and 
> other information are not warranted as to completeness or accuracy and 
> are subject to change without notice. Any comments or statements made 
> herein do not necessarily reflect those of JPMorgan Chase & Co., its 
> subsidiaries and affiliates. This transmission may contain information 
> that is privileged, confidential, legally privileged, and/or exempt 
> from disclosure under applicable law. If you are not the intended 
> recipient, you are hereby notified that any disclosure, copying, 
> distribution, or use of the information contained herein (including 
> any reliance thereon) is STRICTLY PROHIBITED. Although this 
> transmission and any attachments are believed to be free of any virus 
> or other defect that might affect any computer system into which it is 
> received and opened, it is the responsibility of the recipient to 
> ensure that it is virus free and no responsibility is accepted by 
> JPMorgan Chase & Co., its subsidiaries and affiliates, as applicable, 
> for any loss or damage arising in any way from its use. If you 
> received this transmission in error, please immediately contact the 
> sender and destroy the material in its entirety, whether in electronic 
> or hard copy format. Thank you. Please refer to 
> http://www.jpmorgan.com/pages/disclosures for disclosures relating to 
> UK legal entities.
>


-- 
Mango Solutions
data analysis that delivers

Tel:  +44(0) 1249 76 77 00
Fax:  +44(0) 1249 76 77 07
Mob:  +44(0) 7813 52 61 23



More information about the R-help mailing list