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
?
格状图的更新方法允许用户在初始调用后修改格状图。但是更新行为更像是替换而不是附加。这不同于ggplot2习语,其中每个新层都是现有层的附加层。是否有可能使用晶格来得到这个相加行为?
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)
Now I want to add panel.text
to the existing plot. Using update
in the following way doesn't work:
现在我要添加面板。文本到现有的情节。以以下方式使用update行不通:
update(LL, panel=function(...){
args <- list(...); panel.text(args$x, args$y+2, round(args$y, 0))
})
I know that I can use update
by specifying all of the layers in the panel function:
我知道我可以通过指定面板函数中的所有层来使用update:
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
?
问:是否有办法在update.trellis中添加现有的面板?
2 个解决方案
#1
13
See layer
from the latticeExtra
package.
从latticeExtra包中查看图层。
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))
#2
3
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.
这里有一种不用格子的方法。诚然,这比格子间的额外路线要复杂和困难得多。然而,这个格子的灵活性。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))
}
#1
13
See layer
from the latticeExtra
package.
从latticeExtra包中查看图层。
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))
#2
3
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.
这里有一种不用格子的方法。诚然,这比格子间的额外路线要复杂和困难得多。然而,这个格子的灵活性。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))
}