0

I made a ggplot with my data. than I wanted to label some interactions between my boxplot-data (via anova/Tukey) to show, if there are signifikant differences between special Groups or not. I'm working with one csv-sheet called "test", that I have imported and with one tabel called "final"

[enter image description here

[enter image description here

but when I plot my data WITH the label from my TukeyHSD, I get every label triple with empty spaces.

enter image description here

how can i remove the empty spaces. there must be any solution to just show the first tree lines with label in the first plot, the middle tree lines in the middle plot with their specific label and the last tree lines in the third spot with label. How can I remove those empty lines with their alphateic labels?

Here my code:

test$int<-interaction(test$geno,test$n)
generate_label_df <- function(TUKEY, variable){
Tukey.levels <- TUKEY[[variable]][,4]
Tukey.labels <- data.frame(multcompLetters(Tukey.levels)['Letters'])
Tukey.labels$int=rownames(Tukey.labels)
Tukey.labels=Tukey.labels[order(Tukey.labels$int) , ]
return(Tukey.labels)
}
model=lm(test$amino~test$int ) ###
ANOVA=aov(model)
TUKEY <- TukeyHSD(x=ANOVA, 'test$int', conf.level=0.95)
labels<-generate_label_df(TUKEY , "test$int")
names(labels)<-c('Letters','int')
yvalue<-aggregate(amino~int, data=test, max)
final<-merge(labels,yvalue) 

ggplot(test, aes(x=int, y=amino)) + 
  stat_boxplot(geom="errorbar") + 
  geom_boxplot(aes(fill=CO2)) + 
  geom_text(data = final, aes(label = Letters, vjust = -0.6)) +
  labs(title = "Aminosäuren Erdkultur", x = "Genotyp", y = "Aminosäurekonz. [µmol/gFW]") +
  scale_x_discrete(labels = c("col.Ammonium" = "col", "sps.Ammonium" = "sps",
                              "swe.Ammonium" = "swe", "col.Nitrat" = "col", 
                              "sps.Nitrat" = "sps", "swe.Nitrat" = "swe",
                              "col.NON" = "col", "sps.NON" = "sps","swe.NON" = "swe"),
                   limits = c("col.Ammonium", "sps.Ammonium", "swe.Ammonium", 
                              "col.Nitrat", "sps.Nitrat","swe.Nitrat", "col.NON", 
                              "sps.NON", "swe.NON") )+
  guides(fill = guide_legend(title = "CO2-Behandlung")) +
  theme(plot.title = element_text(face = "bold", size="17", hjust = "0.5")) +
  scale_fill_manual(values = c("violetred3", "steelblue1")) +
  facet_wrap( ~ n) +
  theme(legend.title = element_text(face = "bold"))

my first opinion was, to remove "facet_wrap(~n)". The Problem is solved than, but obviously I'm missing the subdivision on this way

enter image description here

zx8754
  • 52,746
  • 12
  • 114
  • 209
biffi
  • 21
  • 5
  • 2
    Can you supply your data, or similar example data, in a way that is [reproducible](https://stackoverflow.com/a/5963610/2359523)? – Anonymous coward Nov 30 '18 at 21:59
  • 1
    1) You need to make sure your `final` data.frame also has the `n` variable to facet over. 2) You need to set `scales = 'free_x'` inside `facet_wrap`. Can't help further without a reproducible example. – Axeman Nov 30 '18 at 23:30

0 Answers0