How would I do the following?
我该怎么做?
In a single plot, I would like to create multiple box plots, each X variable being a combination of the categorical variables shown below.
在一个图中,我想创建多个方框图,每个X变量都是下面显示的分类变量的组合。
data(mtcars)
# y variable is mpg
mtcars$vs = as.factor(mtcars$vs)
mtcars$cyl = as.factor(mtcars$cyl)
2 个解决方案
#1
2
If you mean a single plot versus all combinations of the two factors (engine shape (V/S) and cyl), something like so:
如果你指的是一个单独的情节,而不是所有两个因素的组合(发动机形状(V/S)和cyl),那么:
with(mtcars,boxplot(mpg~interaction(as.factor(ifelse(vs,"S","V")),as.factor(cyl))))
abline(v=c(2.5,4.5),col=8)
(Assuming I have "S" and "V" - for 'straight' and 'vee' engine configurations - the right way around; if the R implementation is consistent with Hocking's 1976 paper - as described on the first page here - I think it should be right)
(假设我有“S”和“V”——用于“直”和“V”发动机配置——正确的方向;如果R的实现与Hocking 1976年的论文相一致——如这里第一页所述——我认为应该是对的)
#2
1
You mean something like:
你的意思是类似的:
data(mtcars)
y <- mtcars$mpg
vs <- as.factor(mtcars$vs)
cyl <- as.factor(mtcars$cyl)
par(mfrow=c(1,2))
plot(formula = y ~ cyl + vs)
Likewise, you could use the lattice
package this way:
同样,你也可以这样使用晶格包:
require(lattice)
bwplot( ~ y | vs + cyl)
Note: the only problem with this style of plot is that the result is, in effect, 6 plots in one (since there are 2 levels for "vs" * 3 for "cyl"). Hence, unless you have enough data available for each possible combination, the resulting plot might not end up looking so good...
注意:这种类型的情节唯一的问题是,结果实际上是,在一个情节中有6个情节(因为“vs”* 3在“cyl”中有两个级别)。因此,除非你对每种可能的组合都有足够的可用数据,否则结果可能不会看起来那么好……
#1
2
If you mean a single plot versus all combinations of the two factors (engine shape (V/S) and cyl), something like so:
如果你指的是一个单独的情节,而不是所有两个因素的组合(发动机形状(V/S)和cyl),那么:
with(mtcars,boxplot(mpg~interaction(as.factor(ifelse(vs,"S","V")),as.factor(cyl))))
abline(v=c(2.5,4.5),col=8)
(Assuming I have "S" and "V" - for 'straight' and 'vee' engine configurations - the right way around; if the R implementation is consistent with Hocking's 1976 paper - as described on the first page here - I think it should be right)
(假设我有“S”和“V”——用于“直”和“V”发动机配置——正确的方向;如果R的实现与Hocking 1976年的论文相一致——如这里第一页所述——我认为应该是对的)
#2
1
You mean something like:
你的意思是类似的:
data(mtcars)
y <- mtcars$mpg
vs <- as.factor(mtcars$vs)
cyl <- as.factor(mtcars$cyl)
par(mfrow=c(1,2))
plot(formula = y ~ cyl + vs)
Likewise, you could use the lattice
package this way:
同样,你也可以这样使用晶格包:
require(lattice)
bwplot( ~ y | vs + cyl)
Note: the only problem with this style of plot is that the result is, in effect, 6 plots in one (since there are 2 levels for "vs" * 3 for "cyl"). Hence, unless you have enough data available for each possible combination, the resulting plot might not end up looking so good...
注意:这种类型的情节唯一的问题是,结果实际上是,在一个情节中有6个情节(因为“vs”* 3在“cyl”中有两个级别)。因此,除非你对每种可能的组合都有足够的可用数据,否则结果可能不会看起来那么好……