[R] xyplot second y-xis and legend

robbert blonk robbert.blonk at gmail.com
Mon Mar 29 14:20:55 CEST 2010


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

-- 
View this message in context: http://n4.nabble.com/xyplot-second-y-xis-and-legend-tp1695032p1695032.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list