I'm trying to add significance tests' letters from a two way ANOVA in a two faceted graph. The problem is, if a try using geom_text, the function works well until it reaches the level 5 of my plots, i.e., the end of the first facet. After this, it repeats the same 5 values for the second facet, but I need to have new values there.
I have tried annotation and ggpub, but both don't seem to solve the problem.
Here is an example:
The data frame:
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")
ANOVA:
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
See that the letters are repeated in both facets.
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)
So, as said, I'd like to have different letters on each facet.



ggplot. Instead of hard-coding your x, y, and label for each piece of text, just make a data frame of that information and treat it as you would any other data inggplot