I want to add a geom_text
above each boxplot according their original dataset. Though, I cannot separate them properly.
To be more precise, this how my dataset look like:
set.seed(1)
example <- data.frame("db" = c(rep("Db1", 3), rep("Db2", 2), rep("Db1", 3), rep("Db2", 2)),
"variable" = c(rep("Var1", 5), rep("Var2", 5)),
"value" = c(runif(3), (runif(2)+0.5), (runif(3)+1), (runif(2)+1.5)),
"group" = c(rep("a", 3), rep("b", 2), rep("a", 3), rep("b", 2)))
And this is the code I produced:
library(ggplot2)
ggplot(example, aes(variable, value)) +
geom_boxplot(aes(fill = db)) +
scale_fill_manual(values = c("firebrick3", "dodgerblue")) +
scale_color_manual(values = c("firebrick3", "dodgerblue")) +
guides(fill = guide_legend(title = "Dataset", override.aes = list(alpha=1, size = 2))) +
theme_classic() +
geom_text(data = example, aes(x = variable, y = max(value, na.rm = T), label = group),
position = position_dodge(width = 9.99), color = "black") +
facet_wrap( ~ variable, scales="free")
I put an important width
to separate the different labels but they are still overlapping.
My question is then: how can I put the right label above the right boxplot for each facet of my plot ?