根据特定的箱线图闪避geom_points group/color

Dodge geom_points according to specific boxplot group/color

如何将 geom_points 隐藏到它们特定的箱线图组中(由审美颜色提供)?我提供了以下(简化的)示例:

library(ggplot2)

ggplot(diamonds, aes(x = cut, y = carat, color = color)) +
    geom_boxplot(outlier.shape = NA) +
    geom_point(aes(group = color))

其中提供了以下情节:

Boxplot with points not in corresponding boxplot

我想让这些点与它们所属的箱线图颜色(组)对齐。我怎样才能做到这一点?

要躲避您的点并将它们与箱形图对齐,请使用 position = position_dodge(width = .75),其中 .75 是箱形图躲避的默认宽度:

library(ggplot2)

ggplot(diamonds, aes(x = cut, y = carat, color = color)) +
  geom_boxplot(outlier.shape = NA) +
  geom_point(aes(group = color), position = position_dodge(width = .75))