-1

I am not very good at R and am trying to pull together this code that is not quite working out how I would like it to. I would really appreciate any help on this!

I would like to perform TukeyHSD test among treatment groups in individual facets in my ggplot boxplots. Currently though, my figures apply a single TukeyHSD across all the boxplots in the figure and this results in a huge number of groupings as you can see in the figure:

my current plot

As I mentioned, it would be preferable to have TukeyHSD run on the individual Depth separated "0" facet, then "5" facet, then "30" facet separately. Is this possible by modifying the code I have been using?

data1 <- read.delim(file="clipboard")

data1$Treatment <- as.factor(data1$Treatment)
data1$Depth <- as.factor(data1$Depth)

model<- aov(MBC~Treatment*Depth, data=data1)
model
library(emmeans)
library('multcomp')
cld_dat = as.data.frame( cld(emmeans(model,~Depth*Treatment),
                               Letters = letters ) )

ggplot(data1, aes(x=Treatment, y=MBC, fill=Treatment)) +
  geom_boxplot() + 
  ylab("MBC") +
  ggtitle("Melinis") +
  facet_wrap(~Depth,ncol=3) +
  geom_text(data = cld_dat, aes(y = 140, label = .group))

One more question, if this is possible: how would I add another y variable "CB" as a second row identical to how I have the first row variable "MBC"?

Thank you for any suggestions!

        Treatment Depth    MBC        CB
zx8754
  • 52,746
  • 12
  • 114
  • 209
  • 1
    there are several issues with this Q. First, its not reproducible. See this [post](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and revise the Q. Second issue is with the data; looking at the plot suggests that you want to cram several factor levels (categorical variables) into a single plot. Its possible to do what you want but you'll have to preprocess the data like collapse several factor levels into a common one . Take a look at the `gridExtra` package for combining multiple plots into a single plot. – mnm Sep 06 '20 at 01:07
  • Please provide the code to generate the dataset – Reza Sep 06 '20 at 08:30

1 Answers1

1

If I understand correctly which factor has those facet levels, what you need is

 cld(emmeans(model, ~ Treatment | Depth))
Russ Lenth
  • 5,922
  • 2
  • 13
  • 21
  • Thanks! This solution applies a single Tukey test across all 24 groups. I suppose what I realise now is that what I had before was correct in that the Tukey was applied separately to each facet, it's just that the letters weren't restarting at 'a' with each facet but were carrying over the letter sequence from the previous facet. Mainly I want to separate the facets in everyway so they don't mix the Tukey test nor sequence of letters. – Hazel Wassername Sep 08 '20 at 18:52
  • No. My answer does separate Tukey comparisons in each group. What you did was a single Tukey comparison for the set of all combinations of treatments and groups. You facet-wrapped those results but that doesn't change the statistical comparisons that were done. – Russ Lenth Sep 08 '20 at 19:20
  • PS. Yes, the letters are re-used in each group when you do it my way. If you don't want that, you will have to programmatically shift the groupings AFTER the cld call. FWIW, I happen to think that cld groupings are a really lousy way to summarize comparisons, because they highlight NON-findings (failures to find significance) rather than findings. I suggest you consider pwpp() as an auxiliary plot, and leave. the grouping letters from your boxplots. – Russ Lenth Sep 08 '20 at 23:50