I want to do a boxplot in ggpubr, with an added jitter. Unfortunately, it jitters on the y-axis, which really is confusing for the viewer.
Setup:
library(ggpubr)
df <- tibble(
group = rep(c("Group1", "Group2", "Group3"), each = 10),
value = sample(0:3, 30, replace = TRUE) # 300 samples with replacement
)
Code for two graphs:
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)
The jitter graph (with jitter on both x and y axes)
The dotplot graph (with the position reflecting the real Y-axis values)
How do I get ggpubr to only jitter on the x-axis, not on the y-axis?
You could also add the jitter to the add.params
argument of ggboxplot
to add the position_jitter
only horizontally like this:
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)
Created on 2023-09-01 with reprex v2.0.2