I want to create multiple facets of boxplots of my data, which illustrate the amount of each of the chemicals present under various conditions.
I have two categorical variables, M1 and M2, which take the values "small, medium, large" and "low, medium, high" respectively. I want these to form the basis of a 3x3 facet grid.
I then have 8 chemicals A-H which take a numeric value, and I want a boxplot for each chemical on each facet.
I've made a 3x3 facet grid, but only with one chemical on each.
EDIT (data taken from answer) my data looks like the data generated from:
set.seed(1234)
n <- 100
M1 <- sample( c("small", "medium", "large"), n, TRUE)
M2 <- sample( c("low", "medium", "high"), n, TRUE)
tmp <- matrix(sample(100, 8*n, TRUE), ncol = 8)
colnames(tmp) <- LETTERS[1:8]
df <- data.frame(M1, M2)
df <- cbind(df, tmp)
rm(M1, M2, tmp)
My code for my plot:
df %>%
ggplot(aes(x = M1, y = A, fill = M1)) +
geom_boxplot() +
theme_minimal() +
facet_grid(M2 ~ M1)
I think I need to reshape my data, so that y = 'measure' before I do the faceted boxplots but I'm not sure how
I want a 3x3 grid of facets, such that the bottom left would correspond to "small","low", and top right would be "large","high", with 8 box plots on each facet for each of the chemicals A-H.
I want for each facet the y-axis is a numerical measure, the x-axis is discrete label A-H (for the 8 boxplots). For the overall 3x3 grid, the (top) x-axis would be 3 labels, small, medium, large, and the (right) y-axis would be 3 labels, low, medium, high?