R中格子图图例中线上的点

Points on lines in lattice plot legend in R

如何使用线中间显示点的格子绘制图例?

有两个很好的相关答案: Include lines and points in lattice legend plot in Rhttp://r.789695.n4.nabble.com/How-to-overlay-lines-and-rectangles-in-lattice-plot-key-td4727682.html

不幸的是,当我将 space 参数更改为 right:

以外的任何其他参数时
auto.key=list(space="right",lines=TRUE,points=TRUE) # worked fine

auto.key=list(space="left",lines=TRUE,points=TRUE) # error message
auto.key=list(x=0, y=0,lines=TRUE,points=TRUE) # error message

我收到以下错误:

Error in do.call("fun", legend[[i]]$args, quote = TRUE) : 
  second argument must be a list

我该如何解决?

如果解决方案显而易见,我们深表歉意。我是 R 和 lattice 的新手,但已经搜索了几天。 非常感谢您的帮助。

示例:

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
}

library(grid)
library(lattice)
library(latticeExtra)
my.chart <- xyplot(
    Sepal.Length + Sepal.Width ~ Petal.Length + Petal.Width, 
    iris,
    type = c("p", "l"),
    auto.key = list(space="right", lines=TRUE, points=TRUE) # works fine
    #auto.key = list(space="left", lines=TRUE, points=TRUE) # error message

    #  why is this not working?
    #legend = list(right = list(fun = drawComboKey))
    #  forced below by: my.chart$legend$right$fun = "drawComboKey"
)
my.chart$legend$right$fun = "drawComboKey"
plot(my.chart)

您快完成了...请注意,对于 space = "left",您需要将自定义函数 drawComboKey() 应用到 左侧的图例 .这类似地适用于 space = "bottom"(或 "top")。

my.chart = xyplot(Sepal.Length + Sepal.Width ~ Petal.Length + Petal.Width, 
                  data = iris, type = "b",
                  auto.key = list(space="left", lines = TRUE, points = TRUE))

my.chart$legend$left$fun = "drawComboKey" # same position as 'space' above
plot(my.chart)

请注意,当使用 x, y 而不是 space 时,该函数需要传递给 my.chart$legend$inside$fun