问题描述
我想绘制x轴为exon
的小提琴图,但是我想对这些图进行分组.如果只是小提琴,这是可行的,但是当我出于某种原因添加抖动时,它没有响应正确的aes
,而是自己绘制吗?这是一个可复制的代码,其中包含错误的屏幕快照.谢谢!
I would like to plot violin plots where x axis is exon
however I want to group the plots. This works if its just violins however when I add in the jitters for some reason its not responding the correct aes
and is plotting on its own? Here is a reproducible code with a screen shot of the error. thanks!
set.seed(1)
df <- data.frame(
exons = c(rep("e1", 200), rep("e2", 200)),
values = rnorm(400, 200, 40),
group = c(
rep("g1", 75), rep("g2", 75), rep("g3", 50),
rep("g1", 75), rep("g2", 75), rep("g3", 50)
)
)
ggplot(df, aes(y = values, x = exons, fill = group)) +
geom_violin() +
geom_jitter(shape = 16, position = position_jitter(0.07))
因此,如果该图有效,则应该在每个外显子的每个组内绘制点,但是这里显然不是.
so if the plot works the dots should had been plotted within each of the group for each exon, however here it is clearly not.
推荐答案
您可能同时需要position_dodge()
和position_jitterdodge()
library(ggplot2)
ggplot(df, aes(y = values, x = exons, fill = group)) +
geom_violin(position = position_dodge(width = 0.9)) +
geom_point(position = position_jitterdodge(seed = 1, dodge.width = 0.9))
另一个值得一提的选项是 ggbeeswarm
包中的geom_quasirandom()
函数
Another option worth mentioning is geom_quasirandom()
function from the ggbeeswarm
package
library(ggbeeswarm)
ggplot(df, aes(y = values, x = exons, fill = group)) +
geom_violin(position = position_dodge(width = 0.9)) +
geom_quasirandom(dodge.width = 0.9, varwidth = TRUE)
这篇关于ggplot2:如何使点与小提琴图分组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!