我想根据原始数据集在每个箱子图上方添加geom_text
。但是,我无法正确地将它们分开。
更确切地说,这是我的数据集的样子:
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)))
这是我制作的代码:
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")
我放了一个重要的width
来分隔不同的标签,但它们仍然重叠。
我的问题是:如何为我的情节的每个方面在正确的箱形图上方放置正确的标签?