【问题标题】:How to reverse only one y-axis in xyplot of zoo object如何在动物园对象的 xyplot 中仅反转一个 y 轴
【发布时间】:2012-01-23 10:18:13
【问题描述】:

我在创建动物园对象的 xyplot 时遇到了一些麻烦。

我创建了一个简化的例子:

z <- zoo(cbind(a=1:4,b=11:14,c=10:13,d=5:8,e=10:7,f=1:4), 1991:1994)

我正在使用以下代码创建多面板 xyplot:

numLbl <- 4
xx <- seq(from = min(time(z)), to = max(time(z)), length.out = numLbl)
xyplot(z[,c(1,2,4,6)],scales = list(x = list(at = time (z), rot = 90)), layout = c(1,4), aspect = "fill", xlab = "",
panel = function (x,y, ...) {
    panel.abline (v = xx, col = "grey", lty = 3)
    panel.xyplot(x, y, type = "b", col = 1)
})

我想做的是仅在其中一个面板上反转 y 轴。我找到了一个相关的例子:

http://r.789695.n4.nabble.com/lattice-limits-in-reversed-order-with-relation-quot-same-quot-td2399883.html

但是,我无法让预面板与我的示例一起使用。我对lattice很陌生,面板功能可能有一些我不明白的地方。

此外,如果有一种简单的方法可以将其中一个面板作为直方图,将其他面板作为线条,我将非常感谢您提供提示。

非常感谢任何帮助!

【问题讨论】:

  • 我想知道如果您允许使用 ggplot 进行回答,您是否会得到更好的响应?

标签: r lattice zoo


【解决方案1】:

这是一个完成第一项工作的预面板示例:

numLbl <- 4
count <- 0
swap <- 3
xx <- seq(from = min(time(z)), to = max(time(z)), length.out = numLbl)
xyplot(z[,c(1,2,4,6)],scales = list(x = list(at = time (z), rot = 90)),
    layout = c(1,4), aspect = "fill", xlab = "",
panel = function (x,y, ...) {
    panel.abline (v = xx, col = "grey", lty = 3)
    panel.xyplot(x, y, type = "b", col = 1)
},
prepanel = function (x,y, ...) {
    count <<- count + 1

    lims <- list(xlim=c(min(x),max(x)), ylim=c(min(y),max(y)));

    if (swap == count) {
        lims[[2]] = rev(lims[[2]]);
    }
    lims;
}
)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-09
    • 1970-01-01
    • 2012-02-27
    • 1970-01-01
    • 2012-02-27
    • 1970-01-01
    • 2014-03-26
    相关资源
    最近更新 更多