How to reshape data to create facets of boxplots of multiple variables in R

為{幸葍}努か 提交于 2020-04-13 09:40:50

问题


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?


回答1:


Reshape the data with package reshape2, function melt. Then use interaction to define the box groups.

long <- reshape2::melt(df, id.vars = c("M1", "M2"))

ggplot(long, aes(x = M1, y = value, group = interaction(M1, M2), fill = M2)) +
  geom_boxplot() +
  theme(axis.text.x = element_text(angle = 30, hjust = 1)) +
  facet_wrap( ~ variable, scales = "free")

To answer the request in the comment, see if this is what you mean.

ggplot(long, aes(x = variable, y = value, group = interaction(M1, variable), fill = M2)) +
  geom_boxplot() +
  facet_grid(M1 ~ M2, scales = "free")

Test data creation code.

I have coerced M2 to factor in order to have the legend properly ordered.

set.seed(1234)

n <- 100
M1 <- sample( c("small", "medium", "large"), n, TRUE)
M2 <- sample( c("low", "medium", "high"), n, TRUE)
M2 <- factor(M2, levels = c("low", "medium", "high"))
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)


来源:https://stackoverflow.com/questions/53971524/how-to-reshape-data-to-create-facets-of-boxplots-of-multiple-variables-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!