R中格点图图例中直线上的点 如何用点阵绘制一个图例,其中点在直线的中间?
有两个很好的相关答案: 和 不幸的是,当我将空格参数更改为右以外的任何值时:R中格点图图例中直线上的点 如何用点阵绘制一个图例,其中点在直线的中间?,r,key,legend,lattice,R,Key,Legend,Lattice,有两个很好的相关答案: 和 不幸的是,当我将空格参数更改为右以外的任何值时: 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 我得到以下错误: Err
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)
drawComboKey你就快到了。。。请注意,对于space=“left”
,您需要将自定义函数drawComboKey()
应用于左侧的图例。这类似地适用于space=“bottom”
(或“top”
)
请注意,当使用x,y
而不是space
时,需要将函数传递到my.chart$legend$inside$fun
请求帮助时,您应该包括一个简单的示例输入和所需的输出,用于测试和验证可能的解决方案。只要包含一些示例数据和一些我们可以复制/粘贴到R中以获取错误的内容,就可以更容易地帮助您。只是提醒一下,Lattice现在不是最流行的R绘图库。如果您是R新手,可能需要从ggplot2开始。它有更多的用户,更容易获得帮助。
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)