在R中使用3个变量制作Boxplot [英] making Boxplot with 3 variables in r
问题描述
我想在r中创建一个箱形图,但是我很难将箱形图按第三个变量分组。
I want to make a boxplot in r but I struggle with grouping the boxplot by a third variable.
我想在一个y轴上绘制箱形图(称为治疗结果问卷的分数,称为HAMD))-
I want to have boxplots- all on one y-axes (score on a treatment outcome questionnaire), called HAMD)) -
0-5周(称为周)
两个治疗组(称为治疗)两次。
and all twice for two treatment groups (called Treatment).
我用$ p $
I made two boxplots with
boxplot(MP $ HAMD〜MP $ week)
boxplot(MP$HAMD ~ MP$week)
和六个带有
boxplot(MP $ HAMD〜MP $治疗)
boxplot(MP$HAMD ~ MP$Treatment)
但是现在我想要12个箱形图,每个治疗每周一次。
如何在r中执行此操作?
But now I want 12 boxplots together, each per week per treatment. How can I do this in r?
总而言之,非常感谢。
推荐答案
您可以使用 interaction
函数和基本图形来做到这一点:
You can do this with the interaction
function and Base graphics:
boxplot( HAMD ~ interaction(treatment,week), data=MP )
boxplot( HAMD ~ interaction(week,treatment), data=MP )
boxplot( HAMD ~ interaction(week,treatment), data=MP,
at= c(1:6, 8:13) )
这是使用格点软件包的一种选择:
And here is one option using the lattice package:
library(lattice)
bwplot( HAMD ~ week|treatment, data=MP )
bwplot( HAMD ~ treatment|week, data=MP )
bwplot( HAMD ~ treatment|week, data=MP, layout=c(6,1) )
以及使用ggplot2软件包的选项:
And an option using the ggplot2 package:
library(ggplot2)
p <- qplot(interaction(treatment,week), HAMD, data=MP, geom="boxplot")
p
p + aes(fill=week)
这篇关于在R中使用3个变量制作Boxplot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!