[R] Lattice xyplot with two y axis
Richard.Cotton at hsl.gov.uk
Richard.Cotton at hsl.gov.uk
Tue Apr 14 18:16:10 CEST 2009
> 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 inform...{{dropped:20}}
More information about the R-help
mailing list