在ggplot方面添加标签

时间:2019-04-24 14:17:01

标签: r ggplot2

我正在尝试在两面图中添加基于双向方差分析的重要性检验的字母。问题是,如果尝试使用geom_text,则该函数可以正常工作,直到达到我的绘图的第5级,即第一个构面的末尾。此后,它会在第二个方面重复相同的5个值,但我需要在那里添加新值。

我尝试过annotationggpub,但似乎都无法解决问题。

这里是一个例子:

数据框:

ex=structure(list(Trat = c("C", "C", "C", "C", "Ca", "Ca", "Ca", 
"Ca", "N", "N", "N", "N", "NP", "NP", "NP", "NP", "P", "P", "P", 
"P", "C", "C", "C", "C", "Ca", "Ca", "Ca", "Ca", "N", "N", "N", 
"N", "NP", "NP", "NP", "NP", "P", "P", "P", "P"), Ano = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("2009", "2015"), class = 
"factor"), 
Trat.Ano = c("C . 2009", "C . 2009", "C . 2009", "C . 2009", 
"Ca . 2009", "Ca . 2009", "Ca . 2009", "Ca . 2009", "N . 2009", 
"N . 2009", "N . 2009", "N . 2009", "NP . 2009", "NP . 2009", 
"NP . 2009", "NP . 2009", "P . 2009", "P . 2009", "P . 2009", 
"P . 2009", "C . 2015", "C . 2015", "C . 2015", "C . 2015", 
"Ca . 2015", "Ca . 2015", "Ca . 2015", "Ca . 2015", "N . 2015", 
"N . 2015", "N . 2015", "N . 2015", "NP . 2015", "NP . 2015", 
"NP . 2015", "NP . 2015", "P . 2015", "P . 2015", "P . 2015", 
"P . 2015"), Especie = c("Sp", "Sp", "Sp", "Sp", "Sp", "Sp", 
"Sp", "Sp", "Sp", "Sp", "Sp", "Sp", "Sp", "Sp", "Sp", "Sp", 
"Sp", "Sp", "Sp", "Sp", "Sp", "Sp", "Sp", "Sp", "Sp", "Sp", 
"Sp", "Sp", "Sp", "Sp", "Sp", "Sp", "Sp", "Sp", "Sp", "Sp", 
"Sp", "Sp", "Sp", "Sp"), cobertura = c(0, 0, 0, 7, 11, 0, 
8, 314, 0, 0, 0, 0, 451, 1398, 1400, 712, 1305, 1063, 1068, 
161, 0, 0, 0, 0, 810, 325, 511, 678, 0, 0, 0, 0, 326, 1043, 
57, 229, 701, 1488, 62, 987)), row.names = c(1L, 4L, 7L, 
10L, 13L, 16L, 19L, 22L, 25L, 28L, 31L, 34L, 37L, 40L, 43L, 46L, 
49L, 52L, 55L, 58L, 61L, 64L, 67L, 70L, 73L, 76L, 79L, 82L, 85L, 
88L, 91L, 94L, 97L, 100L, 103L, 106L, 109L, 112L, 115L, 118L), class = 
"data.frame")

方差分析:

fitMelinis1<- aov(cobertura~Trat*Ano, data=ex) ## Two Way anova
TukeyMelinis1=TukeyHSD(fitMelinis1) # PairWise test
teste=multcompView::multcompLetters4(fitMelinis1, TukeyMelinis1) # the letters I want in my graph

请注意,字母在两个面上都是重复的。

Paleta=c("#339900", "#FF0000", "#000099", "#DAA523", "#000000")

plotMelinis=ggplot(ex,aes(x=Trat,y=cobertura,fill=Trat))+
geom_boxplot() + scale_fill_manual(values=Paleta)+
ylab("cobertura")+ggtitle("Melinis")+facet_wrap(~Ano,ncol=2)+
geom_text(data = ex, x = 1, y = 1500, label = "c", color = "black", 
family="serif", size = 4)+
geom_text(data = ex, x = 2, y = 1500, label = "bc", color = "black", 
family="serif", size = 4)+
geom_text(data = ex, x = 3, y = 1500, label = "c", color = "black", 
family="serif", size = 4)+
geom_text(data = ex, x = 4, y = 1500, label = "a", color = "black", 
family="serif", size = 4)+
geom_text(data = ex, x = 5, y = 1500, label = "a", color = "black", 
family="serif", size = 4)+
geom_text(data = ex, x = 6, y = 1500, label = "c", color = "black", 
family="serif", size = 4)+
geom_text(data = ex, x = 7, y = 1500, label = "abc", color = "black", 
family="serif", size = 4)+
geom_text(data = ex, x = 8, y = 1500, label = "c", color = "black", 
family="serif", size = 4)+
geom_text(data = ex, x = 9, y = 1500, label = "abc", color = "black", 
family="serif", size = 4)+
geom_text(data = ex, x = 10, y = 1500, label = "ab", color = "black", 
family="serif", size = 4)

因此,正如我所说,我希望每个方面都有不同的字母。

Repeated letters in each facet

2 个答案:

答案 0 :(得分:0)

绘制紧凑字母显示的最简单方法是在包含面和轴变量以及用于绘制的标签的数据集中获取所有内容。

我已经使用 emmeans 软件包进行了这种处理,部分原因是该软件包使将紧凑的字母显示结果轻松放入data.frame中进行绘图非常容易。

这是使用您的模型的示例。我在cld()之后使用emmeans(),默认情况下会对十个组进行Tukey调整。

library(emmeans)

( cld_dat = as.data.frame( cld(emmeans(fitMelinis1, ~ Trat*Ano),
                   Letters = letters ) ) )

   Trat  Ano        emmean       SE df   lower.CL  upper.CL .group
6     C 2015 -2.224887e-13 165.7846 30 -338.57726  338.5773    a  
3     N 2009  4.507505e-14 165.7846 30 -338.57726  338.5773    a  
8     N 2015  7.860379e-14 165.7846 30 -338.57726  338.5773    a  
1     C 2009  1.750000e+00 165.7846 30 -336.82726  340.3273    a  
2    Ca 2009  8.325000e+01 165.7846 30 -255.32726  421.8273    ab 
9    NP 2015  4.137500e+02 165.7846 30   75.17274  752.3273    abc
7    Ca 2015  5.810000e+02 165.7846 30  242.42274  919.5773    abc
10    P 2015  8.095000e+02 165.7846 30  470.92274 1148.0773     bc
5     P 2009  8.992500e+02 165.7846 30  560.67274 1237.8273      c
4    NP 2009  9.902500e+02 165.7846 30  651.67274 1328.8273      c

我现在可以在geom_text()层中使用此数据集来添加标签。我仍然会像您一样将y设置为常量。

ggplot(ex, aes(x=Trat, y=cobertura, fill=Trat)) +
  geom_boxplot() + 
  scale_fill_manual(values=Paleta) +
  ylab("cobertura") +
  ggtitle("Melinis") +
  facet_wrap(~Ano,ncol=2) +
  geom_text(data = cld_dat, aes(y = 1500, label = .group))

enter image description here

您会看到每个字母在每个箱线图上的位置相同(因此某些字母未居中)。如果您希望它们都居中,则可以使用trimws()删除空格。 geom_text()行如下所示:

geom_text(data = cld_dat, aes(y = 1500, label = trimws(.group) ) )

答案 1 :(得分:0)

使用stat_summary

#labels need to be ordered based on the year and Trat to line in the plot

ltlbl <- teste$`Trat:Ano`$Letters
ltlbl <- cbind(ltlbl, as.data.frame(do.call(rbind, str_split(names(ltlbl),":"))))
ltlbl <- ltlbl[order(ltlbl$V2, ltlbl$V1), ]


ggplot(ex,aes(x=Trat,y=cobertura,fill=Trat))+
  geom_boxplot() + scale_fill_manual(values=Paleta)+
  ylab("cobertura")+ggtitle("Melinis")+facet_wrap(~Ano,ncol=2)+
  stat_summary(geom = 'text', label = ltlbl$ltlbl, fun.y = max, aes(y = 1450))

enter image description here