"填充"参数在R中的geom_dotplot中创建重叠点

时间:2015-06-01 20:05:35

标签: r ggplot2 plot

我正在使用geom_dotplot,并希望通过颜色来区分哪个点在一个组中与另一个组中的点。我已成功完成此操作,添加" fill = group"到geom_dotplot()中的aes()。

以下是一些重现错误的示例代码:

set.seed(124)
df <- data.frame(Group = rep(c("control","treatment"),20), Response = sample(1:10,40, replace = T), Recovered = rep(c("no","no","no","no","yes"),4))
ggplot() + geom_dotplot(data = df, aes(x = Group, y = Response, fill = Recovered),binaxis = "y", stackdir = "center", alpha = 0.3) + coord_flip()

但是,现在包不再将一个组中的点堆叠在另一个组中,而是重叠它们,隐藏了一些数据。

我可以设置alpha = 0.5来查看这种重叠发生的位置,但我宁愿让它将所有点并排绘制在一起并简单地为一些点着色。有谁知道怎么做?

我知道我可以将position_dodge设置为少量,但不希望这样做会影响轴的解释。

编辑:dput(df)的输出为:

structure(list(Group = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 
2L), .Label = c("control", "treatment"), class = "factor"), Response = c(1L, 
5L, 6L, 4L, 3L, 3L, 6L, 5L, 10L, 3L, 8L, 9L, 8L, 9L, 5L, 1L, 
6L, 8L, 9L, 1L, 7L, 7L, 1L, 5L, 4L, 3L, 9L, 3L, 9L, 4L, 9L, 4L, 
5L, 9L, 2L, 10L, 2L, 10L, 2L, 4L), Recovered = structure(c(1L, 
1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 
1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 
1L, 2L, 1L, 1L, 1L, 1L, 2L), class = "factor", .Label = c("no", 
"yes"))), .Names = c("Group", "Response", "Recovered"), row.names = c(NA, 
-40L), class = "data.frame")

2 个答案:

答案 0 :(得分:1)

You could add stackgroups=TRUE. Does that give you the effect you were looking for?

ggplot() + geom_dotplot(data = df, 
                      aes(x = Group, y = Response, fill = Recovered), 
                      binaxis = "y", stackdir = "center", alpha = 1,
                      stackgroups=TRUE) + coord_flip()

enter image description here

答案 1 :(得分:0)

jitter有帮助吗?

ggplot() + geom_dotplot(data = df, aes(x = Group, y = Response, fill = Recovered), 
                        binaxis = "y", stackdir = "center", alpha = 0.3, position = "jitter") + coord_flip()

enter image description here