我使用
multcompLetters4()
从对 4 组执行的双向方差分析中获得紧凑的字母显示,我现在尝试将我的数据和字母显示为箱线图。
这是我使用的代码:
数据
data1 <- structure(list(Tx = c("T", "T", "T", "T", "T", "T", "T", "T",
"T", "T", "E", "E", "E", "E", "E",
"E", "E", "E", "E", "E", "T", "T",
"T", "T", "T", "T", "T", "T", "T", "E", "E", "E",
"E", "E", "E", "E", "E", "E",
"E"), Group = c("I", "I", "I", "I", "I", "I",
"I", "I", "I", "I", "I", "I", "I", "I", "I", "I", "I",
"I", "I", "I", "G", "G", "G", "G", "G", "G", "G", "G",
"G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G"
), Nb = c(24,
21, 13, 18, 11, 12, 10, 17, 22, 13, 3, 6, 12, 11, 1, 5, 10, 10,
10, 2, 18, 15, 15, 16, 19, 16, 22, 23, 19, 11, 10, 11, 16, 5,
15, 14, 14, 17, 16)), class = c("tbl_df", "tbl",
"data.frame"), row.names = c(NA, -39L))
获取字母并绘图
aov1 <- aov(Nb~Tx*Group, data=data1)
tukey <- TukeyHSD(aov1)
tukey.cld <- multcompLetters4(aov1, tukey)
cld <- as.data.frame.list(tukey.cld$`Tx:Group`)
cld
summary <- data1 %>%
group_by(Tx,Group) %>%
summarise(
w=mean(Nb),
sd=sd(Nb),
max=max(Nb)) %>%
arrange(desc(w))
summary$Tukey <- cld$Letters
ggplot(data1, aes(x = Group, y = Nb, fill = Tx)) +
geom_boxplot(show.legend = TRUE, outlier.shape = NA) +
geom_text(data = summary, aes(x = Group, label = Tukey, y = max), inherit.aes = FALSE)
如图所示,
geom_text()
无法区分不同的“Tx”,因为它们显示在同一个“组”上,不考虑“Tx”。我也尝试过geom_text(data = summary, aes(x = interaction(Group,Tx)...
,但也不起作用。
有没有办法让
geom_text()
理解应该根据fill
参数显示字母?
ggplot(data1, aes(x = Group, y = Nb, fill = Tx)) +
geom_boxplot(show.legend = TRUE, outlier.shape = NA) +
geom_text(data = summary, position=position_dodge(0.8), aes(x = Group,fill=Tx, label = Tukey, y = max), inherit.aes = FALSE)