Richie, this is perfect! Thank you so much.

All the best,
Kate

On Tue, Apr 14, 2009 at 12:16 PM, <Richard.Cotton@hsl.gov.uk> wrote:

> > I have some data which needs to be plotted with lattice.
> >
> > library(lattice)
> > cars <- c(0.1, 0.3, 0.6, 0.4, 0.9)
> > trucks <- c(0.2, 0.5, 0.4, 0.5, 0.1)
> > drivers<-c(121,145,167,200, 210)
> > year<-c(2005,2006,2007,2008,2009)
> > type<-c("local","local","foreign","foreign","foreign")
> > xyplot(cars+trucks~year|type, data=df3, type="o")
> >
> > Basically, I need to show "drivers" as well on a secondary y-axis. Is
> this
> > possible with lattice and xyplot?
>
> The trick is to use a custom y-scale component to draw the second axis.
> You also need to manually make space between panels using the between
> argument.
>
> #Your data, as before
> library(lattice)
> cars <- c(0.1, 0.3, 0.6, 0.4, 0.9)
> trucks <- c(0.2, 0.5, 0.4, 0.5, 0.1)
> drivers<-c(121,145,167,200, 210)
> year<-c(2005,2006,2007,2008,2009)
> type<-c("local","local","foreign","foreign","foreign")
>
> #Custom y-scale component
> myyscale.component <- function(...)
> {
>  ans <- yscale.components.default(...)
>  ans$right <- ans$left
>  foo <- ans$right$labels$at
>  ans$right$labels$labels <- as.character(200*foo)          #200 is the
> scale factor difference between axes, adjust as necessary
>  ans
> }
>
> #The plot
> xyplot(cars+trucks+drivers/200~year|type,
>   type="o",
>   scales=list(
>      x=list(alternating=FALSE),
>      y=list(relation="free", rot=0)),      # relation="free" separates
> the panels
>   yscale.component=myyscale.component,
>   between=list(x=2),                       #between creates more space
> between the panels - you may need to adjust this value
>   par.settings=list(layout.widths=list(right.padding=6)), #this creates
> more space on the right hand side of the plot
>   ylab=NULL,
>   key=simpleKey(text=c("cars", "trucks", "drivers")))
>
>
> Regards,
> Richie.
>
> Mathematical Sciences Unit
> HSL
>
>
>
> ------------------------------------------------------------------------
> ATTENTION:
>
> This message contains privileged and confidential information intended
> for the addressee(s) only. If this message was sent to you in error,
> you must not disseminate, copy or take any action in reliance on it and
> we request that you notify the sender immediately by return email.
>
> Opinions expressed in this message and any attachments are not
> necessarily those held by the Health and Safety Laboratory or any person
> connected with the organisation, save those by whom the opinions were
> expressed.
>
> Please note that any messages sent or received by the Health and Safety
> Laboratory email system may be monitored and stored in an information
> retrieval system.
> ------------------------------------------------------------------------
>
> ------------------------------------------------------------------------
> Scanned by MailMarshal - Marshal's comprehensive email content security
> solution. Download a free evaluation of MailMarshal at www.marshal.com
> ------------------------------------------------------------------------
>

	[[alternative HTML version deleted]]


