I tried unsuccessfully to solve my problem reading the answers of these posts: Indicating the statistically significant difference in bar graph USING R, How to draw the boxplot with significant level? and Put stars on ggplot barplots and boxplots - to indicate the level of significance (p-value).
I would like to add some lines and labels to show the level of significance in a grouped barplot using R like the ones inside the red rectangle.
Here is a simpler version of the code that I came up:
#### DATA
g <- as.factor(c('Kit1_A', 'Kit2_A', 'Kit1_B', 'Kit2_B','Kit1_C', 'Kit2_C'))
groups <- rep(g, 3)
targets <- c(rep('X', 6), rep('Y', 6), rep('Z', 6))
mean <- c(20.8, 23.8, 21.61667, 23.54583, 22.26250, 25.41250, 20.39583, 23.82917, 20.70000, 23.82917, 21.52083, 24.83333, 20.68750, 24.60000, 20.78750, 24.42083, 22.86667, 25.28750)
sd <- c(1.249251, 1.137451, 2.372480, 2.439704, 2.149715, 1.465997, 1.579936, 0.944777, 2.320555, 1.419932, 2.636766, 2.820217, 2.014647, 1.384187, 2.193378, 1.685869, 3.456228, 2.197052)
df <-data.frame(groups, targets, mean, sd)
#### Barplot
library(ggplot2)
f <- ggplot(df, aes(x=targets, y=mean, fill=groups))
f <- f + geom_bar(position="dodge", stat="identity", colour='black')
f <- f + geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd), width=.2,position=position_dodge(.9))
f <- f + theme(legend.title = element_blank())
f <- f + scale_fill_manual(values=c('#D6EAF8','#5DADE2','#2874A6','#D5F5E3','#58D68D','#239B56'))
f <- f + coord_cartesian(ylim = c(0, 35))
Thanks for any help.
You can calculate a p-value between the two box plots by doing a t-test of all genes of one group vs other. You can combine the two normal groups into one and two tumor groups into one,and then do the t-test .
Maybe this is not the best answer but you could do it with annotate("rect")
/annotate("segment")
and grid.text
or another option would be annotation_custom(grob = linesGrob())
I used your data frame with annotate("rect")
and grid.text
.
The updated code is:
# Add rectangles
f + annotate("rect", xmin = 0.6, xmax = 1.07, ymin = 27, ymax =27, alpha=1,colour = "black")+
annotate("rect", xmin = 0.6, xmax = 0.6, ymin = 26.7, ymax =27, alpha=1, colour = "black")+
annotate("rect", xmin = 1.07, xmax = 1.07, ymin = 26.7, ymax =27, alpha=1, colour = "black")+
annotate("rect", xmin = 0.778, xmax = 1.2, ymin = 29.5, ymax =29.5, alpha=1,colour = "black") +
annotate("rect", xmin = 0.778, xmax = 0.778, ymin = 29.2, ymax =29.5, alpha=1,colour = "black") +
annotate("rect", xmin = 1.2, xmax = 1.2, ymin = 29.2, ymax =29.5, alpha=1,colour = "black") +
annotate("rect", xmin = 0.925, xmax = 1.4, ymin = 32, ymax =32, alpha=1,colour = "black") +
annotate("rect", xmin = 0.925, xmax = 0.925, ymin = 31.5, ymax =32, alpha=1,colour = "black") +
annotate("rect", xmin = 1.4, xmax = 1.4, ymin = 31.5, ymax =32, alpha=1,colour = "black") +
# Second two lines
annotate("rect", xmin = 1.61, xmax = 2.08, ymin = 27, ymax =27, alpha=1,colour = "black")+
annotate("rect", xmin = 1.61, xmax = 1.61, ymin = 26.7, ymax =27, alpha=1, colour = "black")+
annotate("rect", xmin = 2.08, xmax = 2.08, ymin = 26.7, ymax =27, alpha=1, colour = "black")+
annotate("rect", xmin = 1.76, xmax = 2.2, ymin = 29.5, ymax =29.5, alpha=1,colour = "black") +
annotate("rect", xmin = 1.76, xmax = 1.76, ymin = 29.2, ymax =29.5, alpha=1,colour = "black") +
annotate("rect", xmin = 2.2, xmax = 2.2, ymin = 29.2, ymax =29.5, alpha=1,colour = "black")
# Add text
grid.text((paste("p<0.001")),
x = unit(0.15, "npc"), y = unit(0.77, "npc"), just = c("left", "bottom"),
gp = gpar(fontface = "bold", fontsize = 8, col = "black"))
grid.text((paste("p<0.001")),
x = unit(0.185, "npc"), y = unit(0.839, "npc"), just = c("left", "bottom"),
gp = gpar(fontface = "bold", fontsize = 8, col = "black"))
grid.text((paste("p<0.001")),
x = unit(0.233, "npc"), y = unit(0.902, "npc"), just = c("left", "bottom"),
gp = gpar(fontface = "bold", fontsize = 8, col = "black"))
# Second two lines
grid.text((paste("p<0.001")),
x = unit(0.42, "npc"), y = unit(0.77, "npc"), just = c("left", "bottom"),
gp = gpar(fontface = "bold", fontsize = 8, col = "black"))
grid.text((paste("p<0.001")),
x = unit(0.45, "npc"), y = unit(0.839, "npc"), just = c("left", "bottom"),
gp = gpar(fontface = "bold", fontsize = 8, col = "black"))
And the output:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With