我怎样才能让 ggpubr ggboxplot 仅在 x 轴上而不是 y 轴上添加抖动

How can I get ggpubr ggboxplot add jitter to only jitter on x-axis, not y-axis

提问人:flashton 提问时间:9/1/2023 最后编辑:flashton 更新时间:9/1/2023 访问量:76

问:

我想在 ggpubr 中做一个箱线图,并增加抖动。不幸的是,它在 y 轴上抖动,这确实让观众感到困惑。

设置:

library(ggpubr)

df <- tibble(
  group = rep(c("Group1", "Group2", "Group3"), each = 10),
  value = sample(0:3, 30, replace = TRUE) # 300 samples with replacement
)

两个图形的代码:

ggboxplot(df, x = "group", y = "value", add = "jitter", color = "group", palette = "jco") +
  rremove("x.text") +
  rremove("xlab") +
  rotate_x_text(angle = 45)

ggboxplot(df, x = "group", y = "value", add = "dotplot", color = "group", palette = "jco") +
  rremove("x.text") +
  rremove("xlab") +
  rotate_x_text(angle = 45)

抖动图(x 轴和 y 轴均有抖动)

enter image description here

点图(位置反映实际 Y 轴值)

enter image description here

如何让 ggpubr 仅在 x 轴上抖动,而不是在 y 轴上抖动?

r ggplot2 ggpubr 抖动

评论


答:

0赞 flashton 9/1/2023 #1

啊,你可以添加一个 ggplot2 geom_jitter

ggboxplot(df, x = "group", y = "value", color = "group", palette = "jco") +
  rremove("x.text") +
  rremove("xlab") +
  rotate_x_text(angle = 45) +
  geom_jitter(aes(color = group), width = 0.2, height = 0)

enter image description here

0赞 Matt B 9/1/2023 #2

如果您愿意更改,我可以使用 ggplot2 做到这一点:

ggplot(df, aes(x = group, y = value, color = group)) +
  geom_boxplot() +
  geom_point(position = position_jitter(width = 0)) +
  theme_classic() +
  theme(axis.text.x = element_text(angle = 45, vjust = 0.5, hjust=1),
        legend.position = "top",
        text = element_text(size=18)) +
  xlab("") 

enter image description here

评论

0赞 flashton 9/1/2023
感谢您的回答,但我正在寻找专门针对 ggpubr 的解决方案。我正在使用 facet 功能,但我没有将其包含在示例中以保持清晰。
1赞 Quinten 9/1/2023 #3

您还可以将抖动添加到参数中,以添加唯一的水平,如下所示:add.paramsggboxplotposition_jitter

library(ggpubr)

ggboxplot(df, x = "group", y = "value", add = "dotplot", color = "group", palette = "jco",
          add.params = list(position = position_jitter(w = 0.2, h = 0))) +
  rremove("x.text") +
  rremove("xlab") +
  rotate_x_text(angle = 45)

创建于 2023-09-01 with reprex v2.0.2

评论

0赞 Damian 9/23/2023
为了其他用户(对于此示例数据没有区别),但请注意,数据是自动装箱的,因此绘图可能与基础数据不完全相同(显示警告)。dotplot