【问题标题】:Adding several box plots in one将多个箱线图合二为一
【发布时间】:2014-11-12 17:00:09
【问题描述】:

我有一个数据集,其中包含三组不同的个体,我们称它们为绿色、红色和蓝色。然后我有涵盖他们血液中 92 种蛋白质的数据,从中我得到了每组中每个人的读数。

我想大致了解每组每种蛋白质的方差和均值。这意味着我想制作一个多箱线图。

我想在 x 轴上显示不同的蛋白质,在每个蛋白质上方显示三个箱形图(最好是不同的颜色)(每组一个),在 y 轴上显示数字蛋白质重量。

我该怎么做?

我目前正在使用一个数据框,其中各组按行划分,每列中都有不同的蛋白质读数。

尝试添加图片,但显然您需要声望点...

我听说你可以在 reshape2 中使用 melt 命令,但我需要指导如何使用它。

请简化答案。说到R,我不是很有经验。

【问题讨论】:

  • 你尝试过什么融化和重塑?您可以将链接添加到您的图片,并且有更多声誉的人会将其添加到您的问题中。
  • i60.tinypic.com/xria0.png 我只看到其他用户有类似我的问题,已经使用reshape2和ggplot2解决了。我还没有真正尝试过使用它们,因为我不知道如何使它工作:/ //mr.unexperienced

标签: r ggplot2 boxplot reshape2


【解决方案1】:

听着,我意识到当你刚开始的时候事情很令人沮丧,但你必须提出具体和有针对性的问题,让人们愿意并且能够以结构化的方式帮助你。

话虽如此,让我们来看一个结构化的示例。我在这里只使用 9 种蛋白质,但你应该明白。

library(ggplot2)
library(reshape2)

# Setup a data frame, since the question did not provide one...
df <- structure(list(Individual = 1:12, 
                     Group = structure(c(2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L), 
                              .Label = c("Blue", "Green", "Red"), class = "factor"), 
                     Protein_1 = c(82L, 23L, 19L, 100L, 33L, 86L, 32L, 41L, 39L, 59L, 93L, 99L), 
                     Protein_2 = c(86L, 50L, 86L, 90L, 37L, 20L, 26L, 38L, 87L, 81L, 23L, 49L), 
                     Protein_3 = c(81L, 31L, 5L, 10L, 79L, 40L, 27L, 73L, 64L, 30L, 87L, 64L), 
                     Protein_4 = c(52L, 15L, 25L, 12L, 63L, 52L, 60L, 33L, 27L, 32L, 53L, 93L), 
                     Protein_5 = c(19L, 75L, 25L, 14L, 33L, 60L, 73L, 13L, 92L, 92L, 91L, 12L), 
                     Protein_6 = c(33L, 49L, 29L, 58L, 51L, 12L, 61L, 48L, 71L, 18L, 84L, 31L), 
                     Protein_7 = c(84L, 57L, 28L, 99L, 47L, 54L, 72L, 97L, 73L, 46L, 68L, 37L), 
                     Protein_8 = c(15L, 16L, 46L, 95L, 57L, 86L, 30L, 83L, 45L, 12L, 49L, 82L), 
                     Protein_9 = c(84L, 91L, 33L, 10L, 91L, 91L, 4L, 88L, 42L, 82L, 76L, 95L)), 
                .Names = c("Individual", "Group", "Protein_1", "Protein_2", "Protein_3", 
                           "Protein_4", "Protein_5", "Protein_6", "Protein_7", "Protein_8", "Protein_9"), 
                class = "data.frame", row.names = c(NA, -12L))

head(df)
# Individual Group Protein_1 Protein_2 Protein_3 Protein_4 Protein_5 Protein_6 Protein_7 Protein_8 Protein_9
# 1          1 Green        82        86        81        52        19        33        84        15        84
# 2          2  Blue        23        50        31        15        75        49        57        16        91
# 3          3   Red        19        86         5        25        25        29        28        46        33
# 4          4 Green       100        90        10        12        14        58        99        95        10
# 5          5  Blue        33        37        79        63        33        51        47        57        91
# 6          6   Red        86        20        40        52        60        12        54        86        91
?melt
df.melted <- melt(df, id.vars = c("Individual", "Group"))
head(df.melted)
# Individual Group  variable value
# 1          1 Green Protein_1    82
# 2          2  Blue Protein_1    23
# 3          3   Red Protein_1    19
# 4          4 Green Protein_1   100
# 5          5  Blue Protein_1    33
# 6          6   Red Protein_1    86

# First Protein
# Notice I am using subset()
ggplot(data = subset(df.melted, variable == "Protein_1"),
       aes(x = Group, y = value)) + geom_boxplot(aes(fill = Group))

# Second Protein
ggplot(data = subset(df.melted, variable == "Protein_2"),
       aes(x = Group, y = value)) + geom_boxplot(aes(fill = Group))

# and so on...

# You could also use facets
ggplot(data = df.melted, aes(x = Group, y = value)) + 
  geom_boxplot(aes(fill = Group)) +
  facet_wrap(~ variable)

是的,我意识到颜色分组与情节的颜色不一致...我将把它留作练习...您必须愿意多次修补、探索和失败。

【讨论】:

  • 非常感谢杰云西!这对我帮助很大,以后我会更好地更详细地解释我的“数据问题”。 Tack så mycket,正如我们在瑞典所说的那样
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-30
  • 1970-01-01
相关资源
最近更新 更多