When overlaying geom_beeswarm() on top of a dodged geom_boxplot() with preserve = "single", the beeswarm points do not always align with their corresponding boxplots if some x-axis groups have fewer subgroup levels than others.
library(ggplot2)
library(ggbeeswarm)
# toy data: group A has 3 subgroups, group B has 2, group C has 1
df <- data.frame(group = character(),
subgroup = numeric(),
value = numeric(),
stringsAsFactors = FALSE)
add_rows <- function(g, s, n, mean_val, sd = 0.2){
data.frame(group = rep(g, n),
subgroup = rep(s, n),
value = rnorm(n, mean = mean_val, sd = sd))
}
df <- rbind(
add_rows("A", 0, 12, mean_val = 2.0),
add_rows("A", 1, 8, mean_val = 2.4),
add_rows("A", 2, 6, mean_val = 2.8),
add_rows("B", 0, 10, mean_val = 2.1),
add_rows("B", 1, 4, mean_val = 2.6),
add_rows("C", 2, 16, mean_val = 2.5)
)
ggplot(df,
aes(x = group,
y = value,
fill = subgroup,
group = interaction(group, subgroup))) +
geom_boxplot(outlier.shape = NA,
position = position_dodge2(width = 0.8, preserve = "single")) +
geom_beeswarm(pch = 21,
dodge.width = 0.8,
alpha = 0.8,
size = 2.5) +
scale_fill_gradient(low = "white", high = "orange") +
theme_minimal()
Gives this plot. The beeswarm points are not aligned with the "preseved" boxplot (in the left one, the first and third are not centered, which can be seen better in the middle one with two groups):
Hello,
When overlaying geom_beeswarm() on top of a dodged geom_boxplot() with preserve = "single", the beeswarm points do not always align with their corresponding boxplots if some x-axis groups have fewer subgroup levels than others.
Expected behavior:
Beeswarm points should be centered above their corresponding dodged boxplot regardless of whether some x-axis groups have missing subgroups.
Observed behavior:
For groups with fewer subgroups, the beeswarm points are horizontally shifted and no longer overlay the corresponding boxplot.
Gives this plot. The beeswarm points are not aligned with the "preseved" boxplot (in the left one, the first and third are not centered, which can be seen better in the middle one with two groups):
Is there any way of passing a position argument to geom_beeswarm ?