"填"参数在R中的geom_dotplot中创建重叠点 [英] "fill" argument creates overlapping points in geom_dotplot in R

查看:364
本文介绍了"填"参数在R中的geom_dotplot中创建重叠点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用geom_dotplot,并希望通过颜色区分哪些点位于一个组中,而不是另一个组中。我已经通过在geom_dotplot()中为aes()添加了fill = group来成功地完成了这项工作。

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

  set.seed(124)
df < - data.frame(Group = rep(c(control,治疗),20),响应=样本(1:10,40,替换= T),恢复=代表(c(否,否,否,否,是),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)的输出是:

 结构(list(Group =结构(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) = c(对照,处理),等级=因子),应答= 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 (1L,1L,2L,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,1L,1L,2L),class =factor,.Label = c(no,
yes))),.Names = c(Group, 回复,恢复 ),row.names = c(NA,
-40L),class =data.frame)


解决方案

您可以添加 stackgroups = TRUE 。这是否给你你正在寻找的效果?

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


I am using geom_dotplot and want to differentiate by color which points are in one group versus another. I have successfully done this by adding "fill=group" to the aes() in geom_dotplot().

Here is some sample code that reproduces the error:

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()

However, now the package no longer stacks the points in one group alongside the other, but instead overlaps them, hiding some of the data.

I can set alpha = 0.5 to see where this overlap occurs, but I would much rather have it plot all the points alongside each other and simply color some of the points. Does anyone know how to do this?

I know I can set position_dodge to a small amount, but would prefer not to as that messes up axis interpretation.

Edit: output of dput(df) is:

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")

解决方案

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()

这篇关于&QUOT;填&QUOT;参数在R中的geom_dotplot中创建重叠点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆