R:lattice.qq 如何制作处理[x] 与对照的多面板图?

发布于 2024-09-07 19:11:02 字数 673 浏览 11 评论 0原文

我有一个看起来像这样的数据框:

str(Data)
'data.frame':   11520 obs. of  29 variables:
 $ groupname  : Factor w/ 8 levels "Control","Treatment1",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ fCycle     : Factor w/ 2 levels "Dark","Light": 2 2 2 2 2 2 2 2 2 2 ...
 $ totdist    : num  0 67.5 89.8 109.1 58.3 ...
 #etc.

我可以像这样绘制治疗1与控制的单个图:

qq(groupname~totdist|fCycle, data=Data, 
 subset=(groupname=='Control'|groupname=='Treatment1'))

它看起来像这样:

alt text

我想自动制作Treatment2 vs Control ...TreatmentX vs Control 的类似图。这是循环的地方还是格子有更好的方法?

I have a dataframe that looks like this:

str(Data)
'data.frame':   11520 obs. of  29 variables:
 $ groupname  : Factor w/ 8 levels "Control","Treatment1",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ fCycle     : Factor w/ 2 levels "Dark","Light": 2 2 2 2 2 2 2 2 2 2 ...
 $ totdist    : num  0 67.5 89.8 109.1 58.3 ...
 #etc.

I can do a single plot of Treatment1 vs Control like this:

qq(groupname~totdist|fCycle, data=Data, 
 subset=(groupname=='Control'|groupname=='Treatment1'))

It looks like this:

alt text

I'd like to automatically make similar plots of Treatment2 vs Control ...TreatmentX vs Control. Is this the place for a loop or does lattice have a better way?

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

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

发布评论

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

评论(1

み零 2024-09-14 19:11:02

要在单个面板上执行此操作需要进行一些重新排列。首先,我将生成一个与您的结构相同的示例数据集

library(lattice)
Data <- data.frame(groupname = factor(rep(c('Control',paste('Treatment',1:7,sep='')),each = 100)),
                   fCycle = factor(rep(rep(c('Dark','Light'),each = 50),8)),
                   totdist = sample(unlist(iris),800,replace = TRUE))

接下来,添加一个变量来区分治疗和控制(即 "Treatment2" 被重新编码为 "Treatment" 等)

Data$groupname2 <- factor(gsub('[1-9]','',as.character(Data$groupname)))

然后重新排列数据集,以便为每个治疗组提供控制数据的副本

Data2 <- NULL
for(treat in paste('Treatment',1:7,sep='')){
  Data2 <- rbind(Data2,
                 cbind(rbind(Data[Data$groupname == treat,],Data[Data$groupname == 'Control',]),
                       treat))
}

最后我们可以制作所需的图表

qq(groupname2~totdist|fCycle*treat, data=Data2)

如果您想为每个治疗单独绘制图,那么循环会更好

pdf('treatVsContQq.pdf')
for(treat in paste('Treatment',1:7,sep='')){
  print(qq(groupname~totdist|fCycle, data=Data,
     subset=(groupname=='Control'|groupname==treat)))
}
dev.off()

To do this on a single panel takes some re-arranging. First, I'll generate a sample data set with the same kind of structure as yours

library(lattice)
Data <- data.frame(groupname = factor(rep(c('Control',paste('Treatment',1:7,sep='')),each = 100)),
                   fCycle = factor(rep(rep(c('Dark','Light'),each = 50),8)),
                   totdist = sample(unlist(iris),800,replace = TRUE))

Next, add a variable to distinguish between treatment and control (i.e. "Treatment2" is recoded as "Treatment", etc.)

Data$groupname2 <- factor(gsub('[1-9]','',as.character(Data$groupname)))

Then rearrange the data-set so that each treatment group is given a copy of the control data

Data2 <- NULL
for(treat in paste('Treatment',1:7,sep='')){
  Data2 <- rbind(Data2,
                 cbind(rbind(Data[Data$groupname == treat,],Data[Data$groupname == 'Control',]),
                       treat))
}

Finally we can make the desired graph

qq(groupname2~totdist|fCycle*treat, data=Data2)

If you want separate plots for each treatment, then a loop would be better

pdf('treatVsContQq.pdf')
for(treat in paste('Treatment',1:7,sep='')){
  print(qq(groupname~totdist|fCycle, data=Data,
     subset=(groupname=='Control'|groupname==treat)))
}
dev.off()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文