R:显示按分组变量分层的等级图中的值 [英] R: display values in levelplot stratified by a grouping variable
问题描述
在下面这个例子中,我需要显示由分组变量 class
分层的每个面板中的每个单元格的值:
library(lattice)
x< - seq(pi / 4,5 * pi,length.out = 5)$ b $ (sqrt(outer(x ^ 2,y ^ 2,+)))
r2< - as.vector(sqrt(outer(x ^ 2,y ^ 2,/)))
grid1< - grid2< - expand.grid( x = x,y = y)
grid1 $ z <-cos(r1 ^ 2)* exp(-r1 /(pi ^ 3))
grid2 $ z <-cos(r2 ^ 2)* exp(-r2 /(pi ^ 3))
grid <-rbind(grid1,grid2)
grid $ class <-c(rep(addition,length(x) ^ 2),rep(division,length(x)^ 2))
p < - levelplot(z〜x * y | factor(class),grid,
panel =函数(...){
arg< - list(...)
panel.levelplot(...)
panel.text(arg $ x,arg $ y,round (arg $ z,1))})
print(p)
然而,单元格值是叠加的因为小组选项不能区分这两组。我怎样才能让这些值在每个组中正确显示?
使用名为下标
的参数来为不同面板中的数据显示子集。通常情况下,它并不需要知道它,但这不是其中之一。
查看 panel.levelplot
显示它自己处理下标
。 args(panel.levelplot)
表明它在函数的形式参数中,函数的主体显示了它如何使用它们。
格的包装::: ltext.default()
)另一方面,不知道或做任何与下标
的任何事情。从 x
, y
和 z
可以看到data.frame grid
,这就是为什么你看到了你所做的重叠绘图。 要为作为当前面板一部分的值绘制文本,您需要明确使用下标
参数,如下所示:
myPanel< - function x,y,z,...,下标=下标)
panel.levelplot(x = x,y = y,z = z,...,下标=下标)
panel.text (z = 1)($)= $($)
$($)$ b $ lt; - levelplot(z〜 x * y | factor(class),grid,panel = myPanel)
print(p)
In this following example, I need to display the values for each of the cells in each of the panels stratified by the grouping variable class
:
library("lattice")
x <- seq(pi/4, 5*pi, length.out=5)
y <- seq(pi/4, 5*pi, length.out=5)
r1 <- as.vector(sqrt(outer(x^2, y^2, "+")))
r2 <- as.vector(sqrt(outer(x^2, y^2, "/")))
grid1 <- grid2 <- expand.grid(x=x, y=y)
grid1$z <- cos(r1^2)*exp(-r1/(pi^3))
grid2$z <- cos(r2^2)*exp(-r2/(pi^3))
grid <- rbind(grid1, grid2)
grid$class <- c(rep("addition",length(x)^2), rep("division", length(x)^2))
p <- levelplot(z~x*y | factor(class), grid,
panel=function(...) {
arg <- list(...)
panel.levelplot(...)
panel.text(arg$x, arg$y, round(arg$z,1))})
print(p)
However, the cell values are superimposed on each other because the panel option dose not distinguish between the two groups. How can I get the values to display correctly in each group?
Slightly behind the scenes, lattice uses an argument called subscripts
to subset data for display in different panels. Often, it does so without you needing to be aware of it, but this is not one of those cases.
A look at the source code for panel.levelplot
reveals that it handles subscripts
on its own. args(panel.levelplot)
shows that it's among the function's formal arguments, and the function's body shows how it uses them.
panel.text()
, (really just a wrapper for lattice:::ltext.default()
), on the other hand, doesn't know about or do anything with subscripts
. From within a call to panel.text(x,y,z)
, the x
, y
, and z
that are seen are the full columns of the data.frame grid
, which is why you saw the overplotting that you did.
To plot text for the values that are a part of the current panel, you need to make explicit use of the subscripts
argument, like this:
myPanel <- function(x, y, z, ..., subscripts=subscripts) {
panel.levelplot(x=x, y=y, z=z, ..., subscripts=subscripts)
panel.text(x = x[subscripts],
y = y[subscripts],
labels = round(z[subscripts], 1))
}
p <- levelplot(z~x*y | factor(class), grid, panel = myPanel)
print(p)
这篇关于R:显示按分组变量分层的等级图中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!