R - 在 geom_boxplot 之上绘制 geom_point (ggplot2)

R - plotting geom_point on top of geom_boxplot (ggplot2)

我在 ggplot2 中的 geom_boxplot() 层之上绘制 geom_point() 层时遇到问题,我做了一些研究,但似乎没有任何报道正是这种性质的问题。我的数据集中有 3 个因子:namegenotyperegion,我的响应变量是 volume。我有工作代码来生成包含两个图层的图。问题是这些点忽略了 geom_point()fill 因素,但没有忽略 geom_boxplot() 的因素。结果是,对于 name 的每个值,这些点都绘制在一组箱线图的中间。这是我构建情节的代码。

meansPlot = ggplot(data=meansData,aes(x=factor(name), y=volume, fill=factor(genotype)))
meansPlot = meansPlot + 
geom_boxplot() +
geom_point() +
facet_wrap( ~ region, scales='free')

很抱歉没有创建可重现的数据集——我还不太精通模拟数据。如果没有一个简单的答案(我希望有,而且我可能只是遗漏了一些东西),我将添加模拟数据来帮助回答问题。

谢谢!

geom_point() 应该使用 color 属性,而不是 fill 属性(除非您使用不寻常的 shapes)。看看这是否适合你:

meansPlot = ggplot(data=meansData,aes(x=factor(name), y=volume, fill=factor(genotype)), color = factor(genotype))
meansPlot = meansPlot + 
geom_boxplot() +
geom_point() +
facet_wrap( ~ region, scales='free')

我最终基本上解决了它。此代码错开 geom_point()geom_boxplot().

内联
meansPlot = ggplot(data=meansData, aes(x=name, y=volume, fill=genotype, color=genotype))  
meansPlot = meansPlot +
geom_point(position=position_jitterdodge(dodge.width=0.9)) +
geom_boxplot(fill="white", position=position_dodge(width=0.9), alpha=0.5) +
facet_wrap( ~ region, scales='free')

感谢大家的努力。