【问题标题】:ggplot2 grouped boxplot doesn't separate groups for different timepointsggplot2 分组箱线图不会将不同时间点的组分开
【发布时间】:2020-03-08 11:01:18
【问题描述】:

我有一个包含两组的简单数据集,每组在 4 个不同的时间点都有一个值。我想将此数据集显示为随时间分组的箱线图,但 ggplot2 不会分隔时间点。

这是我的数据:

 matrix
    Replicate Line Day Treatment  X A WT     Marker Proportion
            1    C  10       low NA      HuCHuD_Pos       8.62
            2    C  10       low NA      HuCHuD_Pos         NA
            1    C  18       low NA      HuCHuD_Pos      30.50                                                    
            3    C  18       low NA      HuCHuD_Pos         NA
            2    C  18       low NA      HuCHuD_Pos         NA
            1    C  50       low NA      HuCHuD_Pos      26.10
            2    C  50       low NA      HuCHuD_Pos      31.90
            1    C  80       low NA      HuCHuD_Pos      12.70
            2    C  80       low NA      HuCHuD_Pos      26.20
            1    C  10    normal NA      HuCHuD_Pos         NA
            2    C  10    normal NA      HuCHuD_Pos      17.20
            1    C  18    normal NA      HuCHuD_Pos       3.96
            2    C  18    normal NA      HuCHuD_Pos         NA
            1    C  50    normal NA      HuCHuD_Pos      25.60
            2    C  50    normal NA      HuCHuD_Pos      17.50
            1    C  80    normal NA      HuCHuD_Pos      19.00
           NA    C  80    normal NA      HuCHuD_Pos         NA

这是我的代码:

matrix = as.data.frame(subset(data.long, Line == line_single & Marker == marker_single & Day != "30"))

pdf(paste(line_name_single, marker_name_single, ".pdf"), width=10, height=10)
plot <- 
ggplot(data=matrix,aes(x=Day, y=Proportion, group=Treatment, fill=Treatment)) +
geom_boxplot(position=position_dodge(1))   
print(plot)
dev.off()

我做错了什么?

我想要什么

我得到了什么

非常感谢您的帮助!

干杯, 宝拉

【问题讨论】:

标签: r ggplot2 boxplot


【解决方案1】:

编辑:

这就是您问题的minimal reproducible example 的样子:

matrix <- structure(list(Day = c(10L, 10L, 18L, 18L, 18L, 50L, 50L, 80L, 80L, 10L, 10L, 18L, 18L, 50L, 50L, 80L, 80L),
                         Treatment = c("low", "low", "low", "low", "low", "low", "low", "low", "low", "normal", "normal", "normal", "normal", "normal", "normal", "normal", "normal"), 
                         Proportion = c(8.62, NA, 30.5, NA, NA, 26.1, 31.9, 12.7, 26.2, NA, 17.2, 3.96, NA, 25.6, 17.5, 19, NA)),
                    class = "data.frame", row.names = c(NA, -17L))

使用factor 来“离散化”变量Day 的建议答案:

ggplot(data=matrix,aes(x=factor(Day), y=Proportion,  fill=Treatment)) +
  geom_boxplot(position=position_dodge(1)) +
  labs(x ="Day")

解释:如果我们将连续变量传递给箱形图的“x”轴,ggplot2 不会将该轴转换为离散变量。因此,在缺少“分组”变量的情况下,我们只能得到一个框。但是,如果我们将变量转换为离散,例如因子、字符串或日期,我们就会得到所需的行为。

另外,当您使用dputhere 描述的技术之一时,找到和测试答案比尝试使用问题中的数据描述更容易(或者至少我不能' t 弄清楚如何加载示例数据)

附:我认为将data.frame 'matrix' 类的变量命名有点令人困惑,因为matrix 在 R 中是它自己的数据类型... ;)

【讨论】:

  • 太棒了,这就是我想要的,我也会记住你的其他建议。
  • ;) 非常好。很高兴我能帮上忙!
  • @PaulaRocktäschel 如果这回答了您的问题,您可以考虑通过单击复选标记接受答案:)
猜你喜欢
  • 1970-01-01
  • 2020-05-22
  • 2021-08-28
  • 1970-01-01
  • 2020-01-31
  • 1970-01-01
  • 2016-09-14
  • 2013-05-11
  • 2016-06-12
相关资源
最近更新 更多