I've been thinking about this problem for a while but i've been very busy so I didn't have much time to work on it till now. I understand better what your original question was. By default Lattice does not like to stack the points on top of the lines in the legend. That's simply not an easily accessible option. However, If you're willing do dig into grid
(the package Lattice
is built on) you can pretty much do anything. So i've hacked together a solution I think may work for you. This should work when
- You only have one column of values in the legend
- You set
auto.key=(..., points=T, lines=T)
The strategy here is to supply a customized legend drawing function to the plot. I try to reuse as much of the calculations as I can from Lattice. I intercept the grid viewport and make a few changes to merge the lines and points. Here's the customized legend drawing function:
drawComboKey <- function(...) {
key = simpleKey(...)
key = draw.key(key, draw = FALSE)
ngroups <- (length(key$children)-1)/3
#remove points column
key$framevp$layout$ncol <-
key$framevp$layout$ncol-3L
key$framevp$layout$respect.mat <-
key$framevp$layout$respect.mat[,-(3:5)]
key$framevp$layout$widths <-
key$framevp$layout$widths[-(3:5)]
#adjust background
key$children[[1]]$col[2] <-
key$children[[1]]$col[2]-3L
key$children[[1]]$cellvp$layout.pos.col[2] <-
key$children[[1]]$cellvp$layout.pos.col[2]-3L
key$children[[1]]$cellvp$valid.pos.col[2] <-
key$children[[1]]$cellvp$valid.pos.col[2]-3L
#combine lines/points
mylines<-(2+ngroups*2):(1+ngroups*3)
for(i in mylines) {
key$children[[i]]$children <-
gList(key$children[[i-ngroups]]$children, key$children[[i]]$children)
key$children[[i]]$childrenOrder <-
names(key$children[[i]]$children)
key$children[[i]]$col <- key$children[[i]]$col-3L
key$children[[i]]$cellvp$layout.pos.col <-
key$children[[i]]$cellvp$layout.pos.col-3L
key$children[[i]]$cellvp$valid.pos.col <-
key$children[[i]]$cellvp$valid.pos.col-3L
}
key$childrenOrder<-names(key$children)
key
}
To use this function, you must intercept the trellis object and set this as the drawing function for the legend. So, starting with the code from last time
library(lattice)
library(latticeExtra)
parSettings <- list(
layout.widths=list(left.padding=8,rigth.padding=20),
superpose.line=list(lwd=2),
superpose.symbol=list(pch=16)
)
comma_formatter <- function (lim, logsc = FALSE, at = NULL, ...) {
ans <- yscale.components.default(lim = lim, logsc = logsc, at = at, ...)
xxPrime <- as.numeric(ans$left$labels$labels)
ans$left$labels$labels <- formatC(xxPrime, format = "fg", big.mark = ",")
ans
}
D <- xyplot(A2009+A2010+A2011+A2012+A2013+A2014 ~ factor(NM$AMes,unique(NM$AMes)), NM,
type = c("p","l"),
yscale.components = comma_formatter,
auto.key=list(space="right", lines=TRUE,points=TRUE),
par.settings = parSettings,layout=c(1,1),aspect=0.6,
main = "Delta Index",
cex.axis=4,
scales=list(x=list(rot=90,font=2,cex=0.8),y=list(font=2),tick.number=9))
D$legend$right$fun = "drawComboKey"
D;
So you can see in the second to the last line I specified our new drawing function. And that's pretty much it. This function is quite fragile and basically a hack. I cannot guarantee that it will always work if Lattice decides to change how it builds legends or anything like that so use at your own risk. Nevertheless, it's a pretty good example of the kinds of things you can do with trellis objects to customize them.
And here's the result: