Tiago Lima
Tiago Lima

Reputation: 31

Adding significance levels in faceted plots with two results in the same x axis

I know that there are many questions already open to add significance level in ggplot2 in faceted plots. However, as a R beginner, I did not found a solution for my plot. My data (.txt file) is available in this link:

1drv.ms/t/s!AsLAxDXdkA9Mg8oXdJ-qxD5AeB4KAw

There are four columns: three factor levels (temperature, parasitoid species and behavior) and a numeric level (number of parasitism and host-killing).

I run the plot with the code:

    ggplot(mydata, aes(x = temperature, y = value, fill = species)) +  
facet_grid(. ~ behavior) +
stat_summary(fun.y = mean, geom = "bar", position = "dodge", stat="identity") +
stat_summary(fun.data = mean_sdl, fun.args = list(mult = 1) +
labs(x = "Temperature", y = "Nº of parasitized or host-killed larvae") +
theme(legend.position = "bottom", legend.title = element_blank(),  legend.text = element_text(size = 11, face = "italic"))

enter image description here

Now I want to add the significance level in each plot, comparing separately the results in Parasitism and Host-Killing in both temperatures. So I have 6 contrasts in each plot. I tested the option using the function stat_compare_means. However, as advised in this Rblogger tutorial (https://www.r-bloggers.com/add-p-values-and-significance-levels-to-ggplots/) I should make a list telling what I want to compare. But, in this case, I have two results for 25°C and two for 30°C. How I could create this list mentioning all the contrasts? If anybody could clarify for me how I can solve this, it would help me a lot. Thanks.

Upvotes: 3

Views: 1767

Answers (2)

Tiago Lima
Tiago Lima

Reputation: 31

Thanks @thc for your help and patience. As I decided to use letters over the error bars, I had to create a new data.frame for each letter and them add the geom_text. This got it right with the below code, after testing many x and y values to insert the letters in the right point. But certainly it has an easier way to do it that I don't know it.

    sigvals1 <- data.frame(x=0.75,y=222.4, text=c("a"), behavior=c("Parasitism"))
    sigvals2 <- data.frame(x=1.207,y=190.55, text=c("a"), behavior=c("Parasitism"))
    sigvals3 <- data.frame(x=1.75,y=117.7, text=c("b"), behavior=c("Parasitism"))
    sigvals4 <- data.frame(x=2.209,y=103, text=c("b"), behavior=c("Parasitism"))
    sigvals5 <-data.frame(x = 0.75, y = 74.8, text=c("A"), behavior = c("Host-Killing"))
    sigvals6 <-data.frame(x = 1.20, y = 92.97, text=c("B"), behavior = c("Host-Killing"))
    sigvals7 <-data.frame(x = 1.74, y = 49.8, text=c("C"), behavior = c("Host-Killing"))
    sigvals8 <-data.frame(x = 2.196, y = 43, text=c("C"), behavior = c("Host-Killing"))

    plot + 
geom_text(data=sigvals1, aes(x=x,y=y, label=text, fill=NA), hjust=0) + 
      geom_text(data=sigvals2, aes(x=x,y=y, label=text, fill=NA), hjust=0) +
      geom_text(data=sigvals3, aes(x=x,y=y, label=text, fill=NA), hjust=0)+
      geom_text(data=sigvals4, aes(x=x,y=y, label=text, fill=NA), hjust=0) +
      geom_text(data=sigvals5, aes(x=x,y=y, label=text, fill=NA), hjust=0) +
      geom_text(data=sigvals6, aes(x=x,y=y, label=text, fill=NA), hjust=0) +
      geom_text(data=sigvals7, aes(x=x,y=y, label=text, fill=NA), hjust=0) +
      geom_text(data=sigvals8, aes(x=x,y=y, label=text, fill=NA), hjust=0)

Upvotes: 0

thc
thc

Reputation: 9705

stat_compare_means worked "out of the box" (which makes sense, since it's using the grouping defined in the ggplot function.

ggplot(mydata, aes(x = temp, y = value, fill = factor(species))) +  
  facet_grid(. ~ behavior) +
  stat_summary(fun.y = mean, geom = "bar", position = position_dodge(width = 1), stat="identity") +
  stat_summary(fun.data = mean_sdl, geom="errorbar", position = position_dodge(width = 1), width=0.25, stat="identity", fun.args = list(mult = 1)) +
                 labs(x = "Temperature", y = "Nº of parasitized or host-killed larvae") +
                 theme(legend.position = "bottom", legend.title = element_blank(),  legend.text = element_text(size = 11, face = "italic")) + 
  stat_compare_means(method = "t.test")

enter image description here

Alternatively, switch the x and fill variables:

ggplot(mydata, aes(x = factor(species), y = value, fill = factor(temp))) +  
  facet_grid(. ~ behavior) +
  stat_summary(fun.y = mean, geom = "bar", position = position_dodge(width = 1), stat="identity") +
  stat_summary(fun.data = mean_sdl, geom="errorbar", position = position_dodge(width = 1), width=0.25, stat="identity", fun.args = list(mult = 1)) +
  labs(x = "Temperature", y = "Nº of parasitized or host-killed larvae") +
  theme(legend.position = "bottom",  legend.text = element_text(size = 11, face = "italic")) + 
  stat_compare_means(method = "t.test")

enter image description here

Upvotes: 2

Related Questions