如何在多面板莱迪思图形bwplot中添加点? [英] How to add points to multi-panel Lattice graphics bwplot?

查看:72
本文介绍了如何在多面板莱迪思图形bwplot中添加点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用bwplot在莱迪思制作箱须图.我想加分.我想出了如何使用panel.points进行简单绘图的方法.但是,当我使用+outer=TRUE制作多面板bwplot时,我不知道如何在不同的面板中放置不同的点.此示例说明了我尝试的解决方案的问题,该问题不起作用. (为简单起见,它将"x"放在任意点而不是均值.)

I'm making box-and-whisker plots in Lattice with bwplot. I want to add points for means. I've figured out how to do this for simple plots using panel.points. However, when I make a multi-panel bwplot using + and outer=TRUE, I don't know how to put different points in different panels. This example illustrates the problem with my attempted solution, which doesn't work. (For the sake of simplicity, it puts "x"s at arbitrary points rather than at means.)

# make some data:
df <- data.frame(var1=rnorm(20), var2=runif(20), cat=rep(c("Z"),20))
# cat isn't doing anything, but I couldn't get the mwe to work without it.

bwplot(var1 + var2 ~ cat, data=df, pch="|", coef=0, outer=TRUE, 
       panel=function(data, ...){
         panel.bwplot(...)
         panel.points(x=c(.5,-.5), pch="x", cex=2)
       }
      )

(outer=Tvar1 + var2结合使用会导致创建两个面板.请参见下面的一个更简单的示例.)

(outer=T combined with var1 + var2 is what causes two panels to be created. See below for a simpler example.)

我得到的是两个面板,每个面板上都有一个箱须图. "x"放在每个图上.在每个面板中,该图上的.5处都有一个"x".整个过程不再使用第二个面板中的-.5,而是重新开始-莱迪思再次获得第一个点.5.

What I get are two panels, with a box-and-whisker plot in each. "x"s are placed over each plot. In each panel, there is an "x" at .5 over the plot. Rather than using -.5 in the second panel, the process starts over--Lattice grabs the first point, .5, again.

如何在不同面板中的图上显示不同点?

How can I get different points displayed over plots in different panels?

谢谢.

后来补充:我现在意识到,这个问题与+outer=TRUE本身无关.使用常规的莱迪思条件可以得到相同的行为:

Later addition: I realize now that this issue has nothing to do with + and outer=TRUE per se. The same behavior can be gotten using routine Lattice conditioning:

df <- data.frame(var1=rnorm(20), cat=rep("Z",20), cat2=rep(c("M","F"), 10))

bwplot(var1 ~ cat | cat2, data=df, pch="|", coef=0, outer=TRUE, 
           panel=function(data, ...){
             panel.bwplot(...)
             panel.points(x=c(.5,-.5), pch="x", cex=2)
           }
          )

推荐答案

您应使用accesor函数panel.number()为每个面板选择x值:

You should use the accesor function panel.number() to choose the x value for each panel:

df <- data.frame(var1=rnorm(20), cat=rep("Z",20), cat2=rep(c("M","F"), 10))

bwplot(var1 ~ cat | cat2, data=df, pch="|", 
           panel=function(...){
             panel.bwplot(...)
             panel.points(x=c(.5,-.5)[panel.number()], 
                          pch="x", cex=2)
           }
          )

已但是,如果要为均值添加点,则实际上并不需要panel.number.您可以定义panel.mean函数(遵循Deepayan的示例)计算并绘制它们:

Edited: However, if you want to add points for means, you don't really need panel.number. You can define a panel.mean function (following the example by Deepayan) to compute and plot them:

panel.mean <- function(x, y, ...) {
    tmp <- tapply(y, factor(x), FUN = mean)
    panel.points(tmp, pch = 20, ...)
}

bwplot(var1 ~ cat | cat2, data=df,
       panel=function(...){
           panel.bwplot(..., pch='|')
           panel.mean(...)
           }
)

这篇关于如何在多面板莱迪思图形bwplot中添加点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆