ggplot2分组箱线图不会将不同时间点的组分开

Pau*_*hel 4 r ggplot2 boxplot

我有一个简单的数据集,包含两组和每组在 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
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

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()
Run Code Online (Sandbox Code Playgroud)

我做错了什么?

我想要的是

我得到什么

非常感谢您的帮助!

干杯,宝拉

dar*_*rio 5

编辑:

您的问题的最小可重现示例如下所示:

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))
Run Code Online (Sandbox Code Playgroud)

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

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

在此输入图像描述

说明:如果我们将连续变量传递到箱线图的“x”轴,ggplot2则不会将该轴转换为离散变量。因此,由于缺乏“分组”变量,我们只能得到一个框。但是,如果我们将变量转换为离散的东西,比如因子、字符串或日期,我们就会得到所需的行为。

另外,当您使用此处dput描述的技术之一时,找到并测试答案比必须尝试使用​​问题中的数据描述要容易得多(或者至少我不知道如何加载该示例)数据)

PS 我认为命名“矩阵”类的变量有点令人困惑,data.frame因为matrix它是 R 中自己的数据类型...;)

  • ;) 出色的。很高兴我能帮上忙! (2认同)