【发布时间】: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)
提前感谢您的帮助!
【问题讨论】: