如何重塑数据以在R中创建多个变量的箱形图的构面 [英] How to reshape data to create facets of boxplots of multiple variables in R

查看:189
本文介绍了如何重塑数据以在R中创建多个变量的箱形图的构面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为数据创建多个箱形图,以说明各种条件下每种化学物质的含量.

I want to create multiple facets of boxplots of my data, which illustrate the amount of each of the chemicals present under various conditions.

我有两个类别变量M1和M2,它们分别取值小,中,大"和低,中,高".我希望这些构成3x3构面网格的基础.

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.

然后我有8种化学药品A-H,它们具有一个数值,我希望每个方面的每种化学药品都具有箱线图.

I then have 8 chemicals A-H which take a numeric value, and I want a boxplot for each chemical on each facet.

我制作了一个3x3的小平面网格,但每个网格上只有一种化学物质.

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)

我的情节代码:

df %>%
  ggplot(aes(x = M1, y = A, fill = M1)) +
  geom_boxplot() +
  theme_minimal() +
  facet_grid(M2 ~ M1)

我认为我需要重塑数据,以便在进行多面箱线图绘制之前y ='measure',但我不确定如何做

I think I need to reshape my data, so that y = 'measure' before I do the faceted boxplots but I'm not sure how

我想要一个3x3的刻面网格,这样左下角将对应于小",低",右上角将对应于大",高",每个面上的8个箱形图化学药品AH.

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.

我希望每个面的y轴都是数字量度,x轴是离散标签A-H(用于8个箱形图).对于整个3x3网格,(顶部)x轴将为3个标签,分别为小,中,大,而y轴(右侧)将为3个标签,分别为低,中,高?

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?

推荐答案

使用包reshape2,函数melt重塑数据.然后使用interaction定义框组.

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")

测试数据创建代码.

为了让图例正确排序,我已强制M2进行分解.

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)

这篇关于如何重塑数据以在R中创建多个变量的箱形图的构面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆