【发布时间】:2018-08-27 20:11:13
【问题描述】:
如何使用点在线条中间显示点的格子绘制图例?
有两个很好的相关答案: Include lines and points in lattice legend plot in R 和 http://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)
【问题讨论】:
-
寻求帮助时,您应该包含一个简单的reproducible example,其中包含可用于测试和验证可能解决方案的示例输入和所需输出。只需包含一些示例数据以及我们可以复制/粘贴到 R 中以获取错误的内容,就可以更轻松地为您提供帮助。提醒一下,Lattice 并不是当今最流行的 R 绘图库。如果您是 R 新手,您可能想从 ggplot2 开始。它拥有更多用户,并且更容易获得帮助。