【问题标题】:Fill area to match the lines of with various 'type' arguments in lattice填充区域以匹配格子中各种“类型”参数的行
【发布时间】:2015-02-08 19:42:07
【问题描述】:

我知道我可以使用 latticeExtra 中的 panel.xyarea 来用任何颜色填充绘图区域。如果没有在xyplot 中定义type 参数,这样的填充将遵循默认type="p" 的路线:

library(lattice)
library(latticeExtra)
data <- data.frame(time=1:24,value=rnorm(24))
xyplot(value~time, data, 
       panel=function(x,y,...){
             panel.xyarea(x,y,...)
             panel.xyplot(x,y,...)}) 

这将绘制panel.xyarea 和来自panel.xyplot 中默认type="p" 的点。现在当我想更改绘图线的type 时出现问题,例如使其成为步进函数type="S"

xyplot(value~time, data, type="S",
       panel=function(x,y,...){
             panel.xyarea(x,y,...)
             panel.xyplot(x,y,...)}

正如您在上面的示例中看到的,panel.xyarea 不会填充新阶跃函数下方的区域,而是将两个区域重叠绘制。如果我将type="S" 移动到panel.xyarea,它不会改变任何东西——实际上它根本没有注册type 参数,并且因为它不存在而绘图。

有没有一种方法可以绕过这个并让panel.xyarea 填充我定义的任何类型的图 - 无论是阶跃函数 (type="S")、黄土 (type="smooth") 还是回归 (type="r")?或者也许有比panel.xyarea 更好的东西在这种情况下使用?

【问题讨论】:

  • 不,没有简单且完全通用的方法可以做到这一点(无需修改许多不同的 lattice 函数)。您是否对type= 的特定值的不完全通用(但简单)的解决方案感兴趣?
  • 当然,让我们从某个地方开始,拍摄@Josh
  • 好的,我会为type="S"type="smooth"整理几个简单的例子。

标签: r plot panel area lattice


【解决方案1】:

对于type 的每个值,您需要构造一个自定义面板函数。幸运的是,如果您在现有的 lattice 代码上密切建模函数(从查看 panel.xyplot 开始),那应该不会太难。例如,下面的两个自定义面板函数包含很多行代码,但我必须编写的只有几行(用 cmets 标记)。

一旦您定义了面板功能(从图中的代码块中复制它们),就可以像这样使用它们:

library(lattice)
library(latticeExtra)
library(gridExtra)
set.seed(100)
data <- data.frame(time=1:24,value=rnorm(24))

## Filled version of xyplot(..., type="S")
a <- xyplot(value~time, data, panel=panel.filled_S) 
## Filled version of xyplot(..., type="smooth") 
b <- xyplot(value~time, data, panel=panel.filled_smooth) 
grid.arrange(a, b, ncol = 2)

对于type="S"的填充版本:

## Modeled on code in panel.xyplot, which is called when type=S"
panel.filled_S <-
function(x,y, ...) {
    horizontal <- FALSE                  ## Edited (may not want to hardcode)
    ord <- if (horizontal)
        sort.list(y)
    else sort.list(x)
    n <- length(x)
    xx <- numeric(2 * n - 1)
    yy <- numeric(2 * n - 1)
    xx[2 * 1:n - 1] <- x[ord]
    yy[2 * 1:n - 1] <- y[ord]
    xx[2 * 1:(n - 1)] <- x[ord][-n]
    yy[2 * 1:(n - 1)] <- y[ord][-1]
    panel.xyarea(x = xx, y = yy, ...)    ## Edited
    panel.lines(x = xx, y = yy, ...)     ## Edited
}
xyplot(value~time, data, panel=panel.filled_S, type="o")

对于type="smooth"的填充版本:

## Modeled on code in panel.loess, called by panel.xyplot when type="smooth"
panel.filled_smooth <-
function (x, y, span = 2/3, degree = 1, family = c("symmetric",
    "gaussian"), evaluation = 50, lwd = plot.line$lwd, lty = plot.line$lty,
    col, col.line = plot.line$col, type, horizontal = FALSE,
    ..., identifier = "loess")
{
    x <- as.numeric(x)
    y <- as.numeric(y)
    ok <- is.finite(x) & is.finite(y)
    if (sum(ok) < 1)
        return()
    if (!missing(col)) {
        if (missing(col.line))
            col.line <- col
    }
    plot.line <- trellis.par.get("plot.line")
    if (horizontal) {
        smooth <- loess.smooth(y[ok], x[ok], span = span, family = family,
            degree = degree, evaluation = evaluation)
        panel.lines(x = smooth$y, y = smooth$x, col = col.line,
            lty = lty, lwd = lwd, ..., identifier = identifier)
        panel.xyarea(smooth$y, smooth$x, ...)  ## Edited
    }
    else {
        smooth <- loess.smooth(x[ok], y[ok], span = span, family = family,
            degree = degree, evaluation = evaluation)
        panel.lines(x = smooth$x, y = smooth$y, col = col.line,
            lty = lty, lwd = lwd, ..., identifier = identifier)
        panel.xyarea(smooth$x, smooth$y, ...)  ## Edited
    }
    smooth
}

【讨论】:

  • 不知道为什么@Josh 但panel.filled_S 函数在我的机器上运行时不起作用 - 它不会绘制type="S",而是通常的type="p"
  • @GeekOnAcid 猜我复制了错误的代码。我刚刚重新复制到帖子中,它对我有用。你呢?
  • 是的,现在工作正常。您几乎已经剖析了这些功能以使其正常工作,但它实现了目标:)
  • 是的,实际上,对于一次性,我只需执行trace(panel.xyplot, edit=TRUE) 并将像panel.xyarea(xx, yy, ...) 这样的单行直接插入到相关函数中。任何比这更复杂的修改都只需要 b/c,当我们定义一个新的面板函数时,我们已经将代码块从其由 panel.xyplot() 其余部分提供的变量环境中取出。对于更多常规使用,我可能会修改panel.xyplot() 和相关函数,将它们命名为panel.xyplot2() 等。等,并将它们捆绑在自己的导入 latticelatticeExtra 的包中。
猜你喜欢
  • 2020-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-06
  • 1970-01-01
  • 1970-01-01
  • 2023-03-20
相关资源
最近更新 更多