[R] xyplot second y-xis and legend

Felix Andrews felix at nfrac.org
Tue Mar 30 01:20:05 CEST 2010


On 29 March 2010 23:20, robbert blonk <robbert.blonk at gmail.com> wrote:
> Dear list,
>
> I try to set a secondary y-axis in a lattice xyplot. This works. However, I
> am unable to set a proper legend/key together with the 2nd y-axis under
> general xyplot procedures. See example below.
>
> The combination of the par.settings with simpleTheme and other settings
> seems to go wrong.
>
> I couldn't find a solution from previous topics. Any suggestions? thanks in
> advance!
>
> Robbert
>
> #data example
> inp3<-data.frame(group=rep(LETTERS[1:4],4),
> GF=as.vector(sapply(1:4,function(x)rep(x,4))),
> val=c(rep(c(2,3,7,8),2),rep(c(2,3,7,8),2)*1.1))
>
> require(lattice)
> #this works fine, without second y-axis:
> xyplot(val~GF,groups=group, data=inp3,type="o",
>   ylab="Relatedness",
>   xlab="genotyped fraction",
>   par.settings=simpleTheme(pch=c(1,2,3,4),lty=c(1,2),lwd=2,cex=1.5),
>   auto.key=list(draw=TRUE,points=TRUE,lines=TRUE,
>      text=LETTERS[1:4]))
>
> #to set a second y-axis with an other scale:
>
>  # convert data to proper scale:
> inp3[which(inp3$group=="A"|inp3$group=="B"),"val"]<-inp3[which(inp3$group=="A"|inp3$group=="B"),"val"]*3
> # set second y-axis
> myyscale.component <- function(...)
> {
>  ans <- yscale.components.default(...)
>  ans$right <- ans$left
>  foo <- ans$right$labels$at
>  ans$right$labels$labels <- as.character((0.3)*foo)
>  ans
>
> }
>
> #plot with second y-axis. This does not work:
>
> xyplot(val~GF,groups= group, data=inp3,type="o",
>   ylab="CD",
>   xlab="GF",
>   scales=list(y=list(relation="free", rot=0)),
>   yscale.component=myyscale.component,
>   par.settings=list(grid.pars=list(fontfamily="serif",cex=1),
>                     layout.widths=list(right.padding=8),
>                     simpleTheme(pch=c(1,2,3,4),lty=c(1,2),lwd=2,cex=1.5)),
>   auto.key=list(draw=TRUE,points=TRUE,lines=TRUE,
>    text=LETTERS[1:4]),
>   legend = list(right=list(fun = grid::textGrob("AB", x=4,rot = 90))))


'legend' overrides 'auto.key' in xyplot, as mentioned in ?xyplot

You can do it in two steps:

foo <-
xyplot(val~GF,groups= group, data=inp3,type="o",
  ylab="CD",
  xlab="GF",
  scales=list(y=list(relation="free", rot=0)),
  yscale.component=myyscale.component,
  par.settings=list(grid.pars=list(fontfamily="serif",cex=1),
                    layout.widths=list(right.padding=8),
                    simpleTheme(pch=c(1,2,3,4),lty=c(1,2),lwd=2,cex=1.5)),
  auto.key=list(draw=TRUE,points=TRUE,lines=TRUE,
   text=LETTERS[1:4]))

update(foo, legend = list(right=list(fun = grid::textGrob("AB", x=4,rot = 90))))



-- 
Felix Andrews / 安福立
Postdoctoral Fellow
Integrated Catchment Assessment and Management (iCAM) Centre
Fenner School of Environment and Society [Bldg 48a]
The Australian National University
Canberra ACT 0200 Australia
M: +61 410 400 963
T: + 61 2 6125 4670
E: felix.andrews at anu.edu.au
CRICOS Provider No. 00120C
-- 
http://www.neurofractal.org/felix/



More information about the R-help mailing list