避免在ggplot2中使用geom_point进行图覆盖 [英] Avoid plot overlay using geom_point in ggplot2
问题描述
在ggplot2中,geom_point默认为在当前绘图上进行绘图。例如,在调用geom_boxplot之后调用geom_point会导致在箱图上绘制点:
In ggplot2, geom_point defaults to plotting over the current plot. For example, calling geom_point after a call to geom_boxplot results in the points plotted over the boxplot:
ggplot(iris, aes(x = "All", y = Sepal.Length)) +
geom_boxplot() +
geom_point(aes(color=Species), position = "jitter")
有没有办法是将点单独绘制到侧面,而不是在箱线图上绘制?
Is there a way to plot the points separately to the side, rather than over the boxplot?
在我的特殊情况下,我想这样做是因为这些点使图变模糊(即使透明,等等),这个问题与示例数据集无关。
In my particular case I want to do this because the points obscure the plot (even with transparency, etc), a problem that is not an issue with the example dataset here.
推荐答案
您可以通过提供单独的x来分别绘制它们-箱线图和点的值:
You can plot them separately by supplying separate x-values for the boxplot and the points:
ggplot(iris, aes(y = Sepal.Length)) +
geom_boxplot(aes(x="Boxplot")) +
geom_point(aes(x="Points", color=Species),
position = position_jitter(width=0.15, height=0))
另一种选择是按物种使用箱线图。
Another option is to use boxplots by species:
ggplot(iris, aes(y = Sepal.Length)) +
geom_boxplot(aes(x="All Data"), width=0.5) +
geom_boxplot(aes(x="By Species", colour=Species), width=0.5,
position=position_dodge(width=0.6))
这是两个图看起来像:
这篇关于避免在ggplot2中使用geom_point进行图覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!