Create boxes for combining categorical variables

How can I do the following?

In a single plot, I would like to create multiple plots of cells, with each X variable being a combination of the categorical variables shown below.

data(mtcars)
# y variable is mpg
mtcars$vs  = as.factor(mtcars$vs)
mtcars$cyl = as.factor(mtcars$cyl)

      

+3


source to share


2 answers


If you mean one graph versus all combinations of two factors (engine shape (V / S) and cyl), then something like this:

with(mtcars,boxplot(mpg~interaction(as.factor(ifelse(vs,"S","V")),as.factor(cyl))))
abline(v=c(2.5,4.5),col=8)

      



enter image description here

(Assuming I have "S" and "V" - for "straight" and "vee" engine configurations - the correct way to go if the R implementation matches the Hocking 1976 doc as described on the first page here - I think it should be right)

+2


source


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 can use the package lattice

like this:



require(lattice)

bwplot( ~ y | vs + cyl)

      

Note: the only problem with this type of plot is that the result is essentially 6 plots in one (since for cyl there are 2 levels for vs * 3). Therefore, if you don't have enough data for every possible combination, the resulting plot may not look so good ...

+1


source







All Articles