【问题标题】:Grouped bar graph with grouped dot plot of raw values [two data frames] using ggplot2使用ggplot2对原始值[两个数据框]进行分组点图的分组条形图
【发布时间】:2019-02-15 20:24:08
【问题描述】:

我正在尝试创建一个分组条形图,其中每个条形上的原始值都有一个点图。在下面的可重现示例中,两个时间段有三种处理方法,因此按时间段将它们分开,我希望每个时间段有三个条形图。然后在此之后,每个条形图的原始点都绘制在顶部。

现在在实际数据集中,这必须来自两个数据框,因为每个条形的值是独立计算的,并不代表统计摘要可以实现的简单平均值或其他值。我查看了其他示例和类似问题的答案,但鉴于数据来自两个单独的数据框,它们似乎不起作用。此外,在下面的示例中,我非常接近,它只是一根头发。

简单的可重现示例

df.stat <- data.frame("Y" = c(40, 30, 20, 30, 30, 30),"Time" = c(1,1,1,2,2,2), "Treatment" = c(1,2,3,1,2,3))

df.raw <- data.frame("Y.raw" = c(35,40,45,25,30,35,15,20,25,25,30,35,10,20,40,30,30,30),"Time.raw" = c(1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2), "Treatment.raw" = c(1,1,1,2,2,2,3,3,3,1,1,1,2,2,2,3,3,3))


Now if you run the code below it seems group does not produce the intended result, however, if you use fill instead of group you get the exact graph I was intending except the issue is the dots color varies per treatment instead of them all having one color across all groups.

ggplot() + geom_col(data = df.stat, aes(x = factor(df.stat$Time), y = df.stat$Y, fill = factor(df.stat$Treatment)), 
                    color = "black", size = 1, width = .8, position = "dodge") +
          geom_dotplot(data = df.raw, aes(x = factor(df.raw$Time.raw), y = df.raw$Y.raw, group = factor(df.raw$Treatment.raw)),
                      alpha = .8, position_dodge(width = .8), binaxis = "y", 
                      stackdir = "center", stackratio = 1)

提前感谢您的帮助!

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    这似乎有效,使用interaction 澄清点图应该被处理和时间避开。 (一个提示,如果你在 ggplot aes() 调用中使用 $ 符号有时会遇到麻烦;单独引用列名更安全。)

    ggplot() + 
      geom_col(data    = df.stat, aes(x = factor(Time), y = Y, 
                                      fill = factor(Treatment)), 
               color = "black", size = 1, width = .8, position = "dodge") +
      geom_dotplot(data = df.raw, aes(x = factor(Time.raw), y = Y.raw, 
                                      group = interaction(factor(Time.raw),factor(Treatment.raw))),
                   alpha = .8, position_dodge(width = .8), binaxis = "y", 
                   stackdir = "center", stackratio = 1)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-11
      • 2018-08-09
      • 1970-01-01
      • 1970-01-01
      • 2020-12-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多