【问题标题】:Multiple boxplots for multiple conditions in RR中多个条件的多个箱线图
【发布时间】:2013-10-31 11:17:14
【问题描述】:

我了解如何从其他几篇文章中在一张图中绘制多个箱线图。但是我有这种情况,无法将多个条件一起绘制。我应用了与我之前的帖子 (Multiple boxplots in R) 相同的想法,但不适用于这种情况。

我有这个数据集

     Control                      Treatment
      L1    L2   L3  L4   L5        S1   S2    S3  S4  S5    
g1   10.5    12  10  11   12        13   14    10  11  12 
g2    11     13  10  10   11        10.5 12    8   9   10
g3    10     9   9   8    9         11   10    11  9   11
g4    9      8   8   9    8         6     5    5   7   6
g5    16     4   6.5 6.8  5         4     6    6   8   9
g6    11     12  7.8 7.5  6         5     4    9   10  11
g7    10     6   8.9 6.4  7.2       13    12   12  12  10
g8    5      4   9.0 5.6  7.8       12    12   9   8   7 
g9    11     12  11  8.5  7.4       10    11.5 8   7   6   
g10   8.9    7.8 13  5.6  6.8       7.6   5.8  5   4   5 

并希望将多个条件表示为同一图表中的多个箱线图。

我想制作第一个图比较 L1 从对照到治疗中的 S1 和 S2,第二个图比较 L2 和 L3 从控制到 S3、S4、S5 治疗和第三个图 L4 和 L5 与 S4 比较和 S5 在治疗中。

这个多条件箱线图怎么可能?还是应该分别制作这些箱线图,然后将它们放在同一张图中?

【问题讨论】:

    标签: r ggplot2 data-visualization lattice boxplot


    【解决方案1】:

    我不确定这是否是您要查找的内容,但它需要进行一些数据操作。

    如果您想在 (L1,S1,S2 | L2,L3,S3,S4,S5 | L4,L5,S4,S5) 的第四列(即“Group2”)中手动输入分组,您将需要复制 S4 和 S5 行并将它们放在适当的组中。然后你会改变:

    facet_wrap( ~ Group2, scales = 'free')
    

    --

    library(ggplot2)
    library(reshape2)
    
    control <- ## read in control data
    control$group <- rep('control', nrow(control))
    control <- melt(control, id.vars = 'group')
    
    treatment <- ## read in control data
    treatment$group <- rep('treatment', nrow(treatment))
    treatment <- melt(treatment, id.vars = 'group')
    
    allData <- rbind(control, treatment)
    
    ggplot(allData, aes(x = variable, y = value, group = variable)) +
      geom_boxplot() +
      facet_wrap( ~ group, scales = 'free')
    

    -- 更新--

    library(gdata)
    library(reshape2)
    library(ggplot2)
    
    control <- ## read in control data
    control$group <- rep('control', nrow(control))
    control <- melt(control, id.vars = 'group')
    
    treatment <- ## read in treatment data
    treatment$group <- rep('treatment', nrow(treatment))
    treatment <- melt(treatment, id.vars = 'group')
    
    allData <- rbind(control, treatment)
    
    compA <- subset(allData, 
                  variable == 'L1' | 
                  variable == 'S1' | 
                  variable == 'S2')
    compB <- subset(allData, 
                  variable == 'L2' | 
                  variable == 'L3' | 
                  variable == 'S3' | 
                  variable == 'S4' | 
                  variable == 'S5')
    compC <- subset(allData, 
                  variable == 'L4' | 
                  variable == 'L5' | 
                  variable == 'S4' | 
                  variable == 'S5')
    
    allData <- combine(compA, compB, compC)
    
    ggplot(allData, aes(x = variable, y = value, group = variable, fill = group)) +
      geom_boxplot() +
      facet_wrap( ~ source, scales = 'free_x')
    

    【讨论】:

    • 这是一种方法,但我想在同一个情节中比较 L1 与 S1、S2 和 L2、L3 与 S3、S4、S5 和 L4、L5 与 S4、S5。
    • 我想我知道你现在要做什么了。我们可能可以通过填充变量传递组(控制或治疗)以突出显示它。我必须处理一些事情,但我可以在大约一个小时内再破解一次。感谢您接受我的回答!
    猜你喜欢
    • 1970-01-01
    • 2012-10-18
    • 1970-01-01
    • 2020-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-27
    • 1970-01-01
    相关资源
    最近更新 更多