Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R ggplot2向抖动位置添加偏移_R_Plot_Ggplot2 - Fatal编程技术网

R ggplot2向抖动位置添加偏移

R ggplot2向抖动位置添加偏移,r,plot,ggplot2,R,Plot,Ggplot2,我有这样的数据 df = data.frame(x=sample(1:5,100,replace=TRUE),y=rnorm(100),assay=sample(c('a','b'),100,replace=TRUE),project=rep(c('primary','secondary'),50)) 我用这个代码画了一幅图 ggplot(df,aes(project,x)) + geom_violin(aes(fill=assay)) + geom_jitter(aes(shape=assa

我有这样的数据

df = data.frame(x=sample(1:5,100,replace=TRUE),y=rnorm(100),assay=sample(c('a','b'),100,replace=TRUE),project=rep(c('primary','secondary'),50))
我用这个代码画了一幅图

ggplot(df,aes(project,x)) + geom_violin(aes(fill=assay)) + geom_jitter(aes(shape=assay,colour=y),height=.5) + coord_flip()
这给了我这个

这是我想要的90%的方式。但我希望每个点只绘制在匹配分析类型的小提琴图的顶部。也就是说,点的jitterred位置设置为,对于每个项目类型,三角形仅出现在上部teal小提琴图和底部红色小提琴图中的圆圈上


有什么办法吗?

您可以在分析和项目之间使用
交互作用

p <- ggplot(df,aes(x = interaction(assay, project), y=x)) + 
     geom_violin(aes(fill=assay)) +  
     geom_jitter(aes(shape=assay, colour=y), height=.5, cex=4) 
p +  coord_flip()

p您可以在分析和项目之间使用
交互作用

p <- ggplot(df,aes(x = interaction(assay, project), y=x)) + 
     geom_violin(aes(fill=assay)) +  
     geom_jitter(aes(shape=assay, colour=y), height=.5, cex=4) 
p +  coord_flip()

p为了获得想要的结果,最好使用
position\u jitterdodge
,因为这样可以最好地控制点的“抖动”方式:

ggplot(df, aes(x = project, y = x, fill = assay, shape = assay, color = y)) + 
  geom_violin() + 
  geom_jitter(position = position_jitterdodge(dodge.width = 0.9,
                                              jitter.width = 0.5,
                                              jitter.height = 0.2),
              size = 2) + 
  coord_flip()
其中:


为了获得所需的结果,最好使用
位置抖动道奇
,因为这可以让您最好地控制点“抖动”的方式:

ggplot(df, aes(x = project, y = x, fill = assay, shape = assay, color = y)) + 
  geom_violin() + 
  geom_jitter(position = position_jitterdodge(dodge.width = 0.9,
                                              jitter.width = 0.5,
                                              jitter.height = 0.2),
              size = 2) + 
  coord_flip()
其中: