Add Significance Bars with ggsignif
Finally, we will add significance bars to our grouped bar graph using ggsignif package. We will use the geom_signif() function to add the significance bars.
The significance bars are added to indicate the statistical significance of differences between groups within each facet. The geom_signif() function is used to add these bars. This function takes as input the x and y positions of the bars to which the significance bars should be added, along with the p-values for the statistical significance. It also allows for customization of the appearance of the bars, such as color, size, and style. By adding these significance bars to our grouped bar graph, we can more effectively communicate the differences between groups to our audience.
Example 1:
In this example, we are adding the geom_signif() function to our existing plot. We are defining the comparisons we want to test using the list argument. In this case, we are comparing the mean mpg of cars with 4 cylinders and 3 gears to those with 4 cylinders and 5 gears and labeling it with “*” and comparing the mean mpg of cars with 4 cylinders and 5 gears to those with 5 cylinders and 5 gears and labeling it with “”. We are also setting the text size and vertical justification of the annotations.
R
# Load the ggsignif package library (ggsignif) # Create a grouped bar graph using ggplot2 ggplot (cyl_gear_data, aes (x = factor (gear), y = mean_mpg, fill = factor (cyl))) + # Add bars to the plot geom_bar (stat = "identity" , position = "dodge" ) + # Add axis labels labs (x = "Gear" , y = "Mean MPG" ) + # Add plot title ggtitle ( "Mean MPG by Gear and Number of Cylinders" ) + # Set black and white theme theme_bw () + # Add facet_wrap for separate plots by number of cylinders facet_wrap (~cyl, ncol = 2) + # Add significance bars and annotations geom_signif (comparisons = list ( c ( "4" , "3" ), c ( "4" , "5" )), # Use asterisks as annotations annotations = c ( "*" , "" ), # Adjust text size and vertical justification textsize = 5, vjust = -0.5) |
Output:
Example 2:
We will be adding significance levels for the first graph as well. We specified the comparisons and annotations for each plot, as well as adjusted the text size, vertical justification, and horizontal positioning of the bars and annotations as necessary.
R
# Load the ggsignif package library (ggsignif) # Create a grouped bar graph using ggplot2 ggplot (cyl_gear_data, aes (x = factor (gear), y = mean_mpg, fill = factor (cyl))) + # Add bars to the plot geom_bar (stat = "identity" , position = "dodge" ) + # Add axis labels labs (x = "Gear" , y = "Mean MPG" ) + # Add plot title ggtitle ( "Mean MPG by Gear and Number of Cylinders" ) + # Set black and white theme theme_bw () + # Add facet_wrap for separate plots by number of cylinders facet_wrap (~cyl, ncol = 2) + # Add significance bars and annotations geom_signif (comparisons = list ( c ( "4" , "3" ), c ( "4" , "5" ), c ( "3" , "5" )), # Use asterisks and additional significance levels as annotations annotations = c ( "***" , "**" , "*" ), # Adjust text size and vertical justification textsize = 5, vjust = -0.5) |
Output:
Grouped Bar Graphs and Facet_Wrap in R
In this article, we are going to learn how to define data when using ggsignif with grouped bar graphs and facet_wrap in R programming language.
ggplot2 is a popular R Language package used for data visualization. It allows users to create a wide range of plots and graphs, including bar graphs. However, adding statistical significance bars to bar graphs can be a bit tricky. That’s why ggsignif, another R package, comes in handy. ggsignif provides an easy way to add significance bars to bar graphs created with ggplot2. In this article, we will explore how to define data when using ggsignif with grouped bar graphs and facet_wrap in R.
Installing required packages
Execute the below commands to install the dplyr, ggplot2, and ggsignif packages in R respectively.
install.packages("dplyr") install.packages("ggplot2") install.packages("ggsignif")