多个y变量的晶格中的条件面板函数 [英] conditional panel function in lattice for multiple y variables

查看:70
本文介绍了多个y变量的晶格中的条件面板函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据集,其中值(mean)可以或不能落在lower.boundupper.bound给定的间隔内.我想使用lattice绘制此图,并取得了非常不错的成绩,但仍然缺少三件事,我不知道该如何解决(我对lattice相对陌生).

I have a dataset in which a value (mean) can or cannot fall within an interval given by lower.bound and upper.bound. I would like to plot this using lattice and have achieved something really nice, but there are still three things missing, I don't know how to tackle (I am relatively new to lattice).

df <- read.table("http://pastebin.com/raw.php?i=FQh6F12t")

require(lattice)
lattice.options(default.theme = standard.theme(color = FALSE))

head(df)
##    code topic problem mean lower.bound upper.bound consistent
## 7  A04C  coke      MP 99.5       36.45       95.95          0
## 8  A04C  coke      MT 47.5       22.78      100.00          1
## 11 A04C  girl      MP 50.0        4.75        9.75          0
## 12 A04C  girl      MT 99.5       20.00      100.00          1
## 23 A14G  coke      MP 88.5       21.25       66.75          0
## 24 A14G  coke      MT 82.5       48.36      100.00          1    

dotplot(lower.bound + mean + upper.bound ~ code | problem * topic, 
       data = df, pch = c(6, 3, 2), scales = list(x = list(draw = FALSE)),
       as.table = TRUE)

这将产生:

向下箭头/三角形指示下限,向上箭头/三角形指示上限,+标记mean.我想添加到绘图中的以下内容,但不知道如何添加(显然是自定义面板功能):

The down-arrows/triangles indicate the lower bound, the up arrows/triangles indicate the upper bound and the + marks the mean. The following things I would like to add to the plot but have no idea how (besides obviously customizing the panel function):

  1. 有条件的pch取决于mean值是否在间隔内.变量consistent表示此值(0 =间隔外).对于内部值,pch应该为1,对于间隔之外的值,3应该为. (下限和上限的pch应该保持不变)
  2. 标记间隔.我想在每个x轴刻度线的lower.boundupper.bound之间画一条粗线.
  3. 将间隔以外的值的比例添加到面板标题中(例如,将MP; 58.6%添加到左上角的面板中).
  1. Conditional pch based on whether or not a mean value is inside the interval. The variable consistent indicates this (0 = outside the interval). pch should be 1 for values inside and 3 for values outside the interval. (pch for the lower- and upper bound should remain unchanged)
  2. Marking the interval. I would like to draw a thicker line between the lower.bound and upper.bound at each x-axis tick.
  3. Add the proportion of values outside the interval to the panel headers (e.g., MP; 58.6% to the panel in the upper left corner).

对于1和2,我的问题显然是,当拥有多个y变量时,我不知道如何处理自定义面板函数(即,如何基于此编写条件面板函数).但是我找不到任何东西.

For 1 and 2 my problem obviously is, that I don't know how to deal with custom panel function when having multiple y variables (i.e., how to write conditional panel functions based on this). But I couldn't find anything on it.

对于3,区间外的值所占比例如下所示:

For 3, the proportion of values outside the interval is given by something like:

1 - with(df, tapply(consistent, list(topic, problem), mean))
##          MP     MT
## coke 0.5862 0.1724
## girl 0.8276 0.1724

如果答案还包括x轴上的水平不错的排序,那肯定是一个加分.顺序可以在每个面板中更改(即,即使在彼此上方的面板中,相同的x轴刻度也可以对应于code的不同级别).但这并不重要.

If the answer would furthermore include a nice ordering of levels on the x-axis that would definitely be a plus. The order can change in every panel (i.e., even in panels above each other the same x-axis tick can correspond to a different level of code). But this is not important.

推荐答案

这不是很漂亮,但是应该可以完成真正的工作(向您展示如何使这种绘图工作).

Well, this isn't real pretty, but it should get the real job (showing you how to get this kind of plot working) done.

基本思想是重写公式,以便它的LHS上没有一堆名称(即lower.bound + mean + upper.bound).该语法等效于指定一个groups=术语,该术语最终会触发panel.superpose(),这很难以您想要的方式进行自定义.

The basic idea is to rewrite the formula so that it doesn't have a bunch of names on its LHS (i.e. lower.bound + mean + upper.bound). That syntax is equivalent to specifying a groups= term, which ends up triggering panel.superpose() which is kind of a pain to customize in the way you want.

相反,我只在LHS上包含mean,然后在自定义面板功能内使用subscripts来分别选择upper.boundlower.bound的匹配元素.

Instead, I just include mean on the LHS, and then use subscripts inside of the custom panel function to pick out in each case the matching elements of upper.bound and lower.bound.

我希望其余的内容可以自我解释:

I'm hoping the rest is pretty self explanatory:

LABS <- LETTERS[1:4]
with(df,
     dotplot(mean ~ code | problem * topic,
             lb=lower.bound, ub=upper.bound, mpch = c(3,1)[consistent+1],
             ylim = extendrange(c(0,100)),
             panel = function(x, y, lb, ub, mpch, ..., subscripts) {
                 panel.dotplot(x, y, ..., pch=mpch[subscripts])
                 lpoints(x, lb[subscripts], pch=6)
                 lpoints(x, ub[subscripts], pch=2)
                 lsegments(x,lb[subscripts],x,ub[subscripts],col="grey60")
                 ltext(x=x[3], y=95, LABS[panel.number()], col="red",fontface=2)
             },
             scales = list(x = list(draw = FALSE)), as.table = TRUE)
     )

这篇关于多个y变量的晶格中的条件面板函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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