1
votes

This is my code. I have two factor variables (cyl and am)that I want to plot in my box plot.

boxplot(mpg~cyl+am,data=mtcars)

enter image description here

I want to make a boxplot for each individual factor levels - 4,6,8,0,1 but not the interaction of them (e.g. 4.0).

My box plot should only have (4,6,8,0,1) on the axis. How do I do that?

1

1 Answers

1
votes

You may reshape the data and then you can do the plotting, However this may not be the elegant

library(reshape2)
mtcars_subset <- mtcars[,c("am", "cyl", "mpg")]
dat <- melt(mtcars_subset, id.vars="mpg")
#or dat <- melt(mtcars, id.vars = "mpg", measure.vars = c("cyl", "am"))
boxplot(data=dat, mpg ~ value)

enter image description here