是否可以更新 R 中的格子面板?

发布于 2024-12-08 05:51:43 字数 1044 浏览 0 评论 0原文

格子图的更新方法允许在初始调用后修改格子图。但更新行为更像是替换而不是追加。这与 ggplot2 习惯用法不同,其中每个新图层都是对现有图层的附加。是否有可能使用lattice获得这种附加行为?

示例:

LL <- barchart(yield ~ variety | site, data = barley,
        groups = year, stack = TRUE,
        between=list(y=0.5),
        scales = list(x = list(rot = 90)))
print(LL)

在此处输入图像描述

现在我想将 panel.text 添加到现有绘图中。按以下方式使用 update 不起作用:

update(LL, panel=function(...){
           args <- list(...); panel.text(args$x, args$y+2, round(args$y, 0))
         })

在此处输入图像描述

我知道我可以通过指定面板函数中的所有图层来使用更新:

update(LL, panel=function(...){
           args <- list(...)
           panel.barchart(...)
           panel.text(args$x, args$y+2, round(args$y, 0))
         })

这可以工作,但要求我知道格子图中已有的内容 - 或者我重构我的代码相当多。

问题:有没有办法添加到update.trellis中的现有面板?

The update method of trellis plots allows one to modify a lattice plot after the initial call. But the update behaviour is more like replace than append. This differs from the ggplot2 idiom where each new layer is additive to what exists already. Is it possible to get this additive behaviour using lattice?

An example:

LL <- barchart(yield ~ variety | site, data = barley,
        groups = year, stack = TRUE,
        between=list(y=0.5),
        scales = list(x = list(rot = 90)))
print(LL)

enter image description here

Now I want to add panel.text to the existing plot. Using update in the following way doesn't work:

update(LL, panel=function(...){
           args <- list(...); panel.text(args$x, args$y+2, round(args$y, 0))
         })

enter image description here

I know that I can use update by specifying all of the layers in the panel function:

update(LL, panel=function(...){
           args <- list(...)
           panel.barchart(...)
           panel.text(args$x, args$y+2, round(args$y, 0))
         })

This will work, but requires that I know what is already in the lattice plot - or that I refactor my code quite substantially.

Question: Is there a way of adding to the existing panel in update.trellis?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝天白云 2024-12-15 05:51:43

请参阅 latticeExtra< 中的 layer /code>包。

library(lattice)
library(latticeExtra)
LL <- barchart(yield ~ variety | site, data = barley,
        groups = year, stack = TRUE,
        between=list(y=0.5),
        scales = list(x = list(rot = 90)))
LL + layer(panel.text(x, y, round(y, 0), data=barley))

代码结果

See layer from the latticeExtra package.

library(lattice)
library(latticeExtra)
LL <- barchart(yield ~ variety | site, data = barley,
        groups = year, stack = TRUE,
        between=list(y=0.5),
        scales = list(x = list(rot = 90)))
LL + layer(panel.text(x, y, round(y, 0), data=barley))

result of code

话少情深 2024-12-15 05:51:43

这是一种无需latticeExtra即可实现的方法。不可否认,它比latticeExtra 路线更复杂、更困难。但是,此 trellis.focus 方法的灵活性在其他上下文中可能更有用。

barchart(yield ~ variety | site, data = barley,
               groups = year, stack = TRUE,
               between=list(y=0.5),
               scales = list(x = list(rot = 90)))

panels  = trellis.currentLayout()
for( i in seq_along(panels) ) {
  ind = which( panels == i, arr.ind=TRUE )
  trellis.focus("panel",ind[2],ind[1])
  vars = trellis.panelArgs()
  panel.text(vars$x,vars$y,round(vars$y,0))
}

上面代码的输出

Here is a way to do it without latticeExtra. Admittedly, it's more complicated and difficult than the latticeExtra route. However, the flexibility with this trellis.focus method might be more useful in other contexts.

barchart(yield ~ variety | site, data = barley,
               groups = year, stack = TRUE,
               between=list(y=0.5),
               scales = list(x = list(rot = 90)))

panels  = trellis.currentLayout()
for( i in seq_along(panels) ) {
  ind = which( panels == i, arr.ind=TRUE )
  trellis.focus("panel",ind[2],ind[1])
  vars = trellis.panelArgs()
  panel.text(vars$x,vars$y,round(vars$y,0))
}

Output of code above

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文