Adding P-Values and Performing Tukey Tests to ggplot Bar Graphs Using stat_compare_means and facet_wrap

Using stat_compare_means with facet_wrap to Add P-Values to ggplot Bar Graphs

In this blog post, we will explore the use of stat_compare_means and facet_wrap in ggplot2 to add p-values to bar graphs. We will also cover how to perform Tukey tests on specific comparisons.

Introduction

ggplot2 is a popular data visualization library in R that provides a grammar of graphics for creating high-quality, publication-ready plots. One of its powerful features is the ability to add statistical information to plots using various functions such as geom_smooth, stat_summarize, and stat_compare_means. In this blog post, we will focus on using stat_compare_means with facet_wrap to add p-values to bar graphs.

Setting Up the Data

To illustrate the concepts discussed in this blog post, let’s start by loading the necessary libraries and setting up a sample dataset. We’ll use the famous iris dataset provided by ggplot2.

library(ggplot2)
data("iris")

melt_iris <- melt(iris)

#set specific comparisons
my_comparison_iris <- list(c("setosa", "versicolor"), c("setosa", "virginica"))

Creating the Bar Graph with stat_compare_means

ggplot(data = melt_iris, aes(x = Species, y = value, fill = Species)) + 
  geom_bar(color = "black", stat = "summary", fun = "mean") +
  geom_point(size = 5) +
  scale_fill_manual(values = c("gray65", "deepskyblue1", "orange")) +
  scale_color_manual(values = c("darkslategray", "darkolivegreen", "darkslateblue", "deeppink4")) +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1),
        legend.position = "top",
        legend.title = element_text(face = "bold", size = 14),
        legend.text = element_text(size = 12),
        strip.text = element_text(size = 10, face = "bold"),
        legend.background = element_rect(size=.5, linetype="solid", color = "black"),
        legend.box = "vertical") +
  scale_y_continuous(expand = expansion(mult = c(0.05, 0.2))) +
  stat_compare_means(comparisons = my_comparison_iris) +
  stat_compare_means(method = "anova", label.y.npc = "top") + 
  facet_wrap(~ variable, ncol = 4, scales = "free_y")

Using label.y.npc to Add P-Values

To add the p-values at the top of each graph, we need to use the label.y.npc argument in the stat_compare_means function. This argument takes a value between 0 and 1 that specifies how much to offset the y-axis from the center.

ggplot(data = melt_iris, aes(x = Species, y = value, fill = Species)) + 
  geom_bar(color = "black", stat = "summary", fun = "mean") +
  geom_point(size = 5) +
  scale_fill_manual(values = c("gray65", "deepskyblue1", "orange")) +
  scale_color_manual(values = c("darkslategray", "darkolivegreen", "darkslateblue", "deeppink4")) +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1),
        legend.position = "top",
        legend.title = element_text(face = "bold", size = 14),
        legend.text = element_text(size = 12),
        strip.text = element_text(size = 10, face = "bold"),
        legend.background = element_rect(size=.5, linetype="solid", color = "black"),
        legend.box = "vertical") +
  scale_y_continuous(expand = expansion(mult = c(0.05, 0.2))) +
  stat_compare_means(comparisons = my_comparison_iris, label.y.npc = 0.95) + 
  stat_compare_means(method = "anova", label.y.npc = 0.5)

Performing Tukey Tests on Specific Comparisons

To perform Tukey tests on specific comparisons, we need to use the method argument in the stat_compare_means function and specify "tukey.honest".

ggplot(data = melt_iris, aes(x = Species, y = value, fill = Species)) + 
  geom_bar(color = "black", stat = "summary", fun = "mean") +
  geom_point(size = 5) +
  scale_fill_manual(values = c("gray65", "deepskyblue1", "orange")) +
  scale_color_manual(values = c("darkslategray", "darkolivegreen", "darkslateblue", "deeppink4")) +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1),
        legend.position = "top",
        legend.title = element_text(face = "bold", size = 14),
        legend.text = element_text(size = 12),
        strip.text = element_text(size = 10, face = "bold"),
        legend.background = element_rect(size=.5, linetype="solid", color = "black"),
        legend.box = "vertical") +
  scale_y_continuous(expand = expansion(mult = c(0.05, 0.2))) +
  stat_compare_means(comparisons = my_comparison_iris, method = "tukey.honest")

Conclusion

In this blog post, we demonstrated how to use stat_compare_means with facet_wrap in ggplot2 to add p-values to bar graphs. We also covered how to perform Tukey tests on specific comparisons using the "tukey.honest" method.


Last modified on 2024-04-11