0

I obtain the boxplot using this dataframe

library(reshape2)
library(ggplot2)
ID <- c("DJ45","DJ46","DJ47","DJ48","DJ49","DJ53","DJ54","DJ55","DJ56","DJ57")
Tool <- c("Tool_A", "Tool_A", "Tool_A", "Tool_A", "Tool_A", "Tool_B", "Tool_B", "Tool_B", "Tool_B", "Tool_B")
Name <- c("CMP", "CMP", "CMP", "CMP", "CMP", "CMP", "CMP", "CMP", "CMP", "CMP")
MS1 <- c(51,55,50,59,50,47,48,42,43,46)
MS2 <- c(13,11,14,11,10,17,18,17,20,21)
MS3 <- c(2,3,2,5,6,4,9,6,4,4)
MS4 <- c(16,13,14,11,16,16,18,16,19,15)
MS5 <- c(3,6,3,6,3,4,4,8,5,4)
MS6 <- c(7,7,5,5,8,9,8,6,6,9)

df1 <- data.frame(ID,Tool,Name,MS1,MS2,MS3,MS4,MS5,MS6)
df2<-melt(df1,id.var=c("ID","Tool","Name"))

p2 <- ggplot(df2, aes(x=factor(Tool),y=value,fill=factor(Tool)))+
      geom_boxplot() + labs(title="CMP") +facet_wrap(~variable)
p2

we get this plot,

enter image description here

I then compare the different tools using t-test and obtain the p values for each measurements like this.

t.test(MS1 ~ Tool, df1)
t.test(MS2 ~ Tool, df1)

I would like to include the p-value for each group in the plots. I referred to this link but confused how to code it to my problem. Kindly provide inputs/direction on how to solve this problem.

Community
  • 1
  • 1
Sharath
  • 2,225
  • 3
  • 24
  • 37

1 Answers1

2

Maybe the fact that you have fill defined in your initial aes call was causing the other solution to fail. In this case, just set fill=NA in the geom_text call to aes.

## Make pvalue data
dat <- data.frame(
    x=1.5, y=55,
    pval=sapply(split(df2, df2$variable), function(x) t.test(value ~ Tool, x)$p.value),
    variable=factor(paste0("MS", 1:6))
)

## Plot
p2 <- ggplot(df2, aes(x=factor(Tool),y=value,fill=factor(Tool)))+
  geom_boxplot() + labs(title="CMP") +
  geom_text(data=dat, aes(x=x, y=y, label=round(pval, 3), fill=NA)) +
  facet_wrap(~variable)
Rorschach
  • 31,301
  • 5
  • 78
  • 129