繁体   English   中英

R中格图图例中线上的点

[英]Points on lines in lattice plot legend in R

如何使用点在线条中间显示的点阵绘制图例?

有两个很好的相关答案: 在 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 和格子的新手,但已经搜索了几天。 非常感谢您的帮助。

例子:

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()应用于left的图例。 这类似地适用于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)

xyplot(1)

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM