Open In App

Grouped Bar Graphs and Facet_Wrap in R

Last Updated : 20 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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")

Prepare the Data

We will start by creating some sample data to work with. We will use the mtcars dataset which is an inbuilt dataset in R. The mtcars dataset contains information about various car models, including the number of cylinders, horsepower, and miles per gallon (mpg). 

R




# Import required library
library(dplyr)
 
# Load dataset
data(mtcars)
 
# Group the data by number of cylinders and gear
cyl_gear_data <- mtcars %>% group_by(cyl,
    gear) %>% summarize(mean_mpg = mean(mpg))


Output: In the code above, firstly we have imported the dplyr package and then used dplyr package to group the mtcars dataset by the number of cylinders and gear. We then calculate the mean mpg for each group.

How to Define Data When Using ggsignif With Grouped Bar Graphs and Facet_Wrap in R

 

Create a Grouped Bar Graph

Next, we will create a grouped bar graph using the ggplot2 package. We will use the geom_bar() function to create the bar graph and facet_wrap() function to create separate plots for each number of cylinders.

R




# Load the ggplot2 package
library(ggplot2)
 
# Create a bar graph with cyl_gear_data
# as the data source
# Define the x-axis as gear,
# y-axis as mean_mpg, and fill as cyl
ggplot(cyl_gear_data,
       aes(x = factor(gear),
           y = mean_mpg, fill = factor(cyl))) +
 
  # Use geom_bar to create a bar graph
  # with position = "dodge" to group the bars
  # Set stat = "identity" to use the
  # data values as bar heights
  geom_bar(stat = "identity",
           position = "dodge") +
 
  # Label the x-axis and y-axis with "Gear" and
  # "Mean MPG", respectively
  labs(x = "Gear", y = "Mean MPG") +
 
  # Set the plot title to "Mean MPG by
  # Gear and Number of Cylinders"
  ggtitle("Mean MPG by Gear and Number of Cylinders") +
 
  # Use theme_bw to set a black and white theme
  theme_bw() +
 
  # Set the plot title's horizontal justification to center
  theme(plot.title = element_text(hjust = 0.5)) +
 
  # Create separate plots for each number
  # of cylinders using facet_wrap
  # Set the number of columns to 2 with ncol = 2
  facet_wrap(~cyl, ncol = 2)


Explanation: In the code above, firstly we are importing ggplot2 library and then we are creating a bar graph with ggplot2. We are using aes() to define the x-axis (gear), y-axis (mean_mpg), and fill (cyl). We are then using geom_bar() to create the bar graph with stat = “identity” and position = “dodge”. We are adding axis labels and a plot title using labs and ggtitle. We are also using theme_bw to set a black-and-white theme and facet_wrap to create separate plots for each number of cylinders. 

Output:

How to Define Data When Using ggsignif With Grouped Bar Graphs and Facet_Wrap in R

 

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:

How to Define Data When Using ggsignif With Grouped Bar Graphs and Facet_Wrap in R

 

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:

How to Define Data When Using ggsignif With Grouped Bar Graphs and Facet_Wrap in R

 

Add Significance Level & Stars to the box plot

The process of adding significance levels and stars to a box plot involves visually indicating the statistical significance of differences between groups within the plot. In the below example, the ggpubr package is used to add significance levels and stars to a box plot of petal length by species in the iris dataset. We can install ggpubr package using below command

install.packages("ggpubr")

Specifically, the stat_compare_means() function is used to add the significance levels and stars. This function computes the specified statistical test (in this case, a t-test) and adds a label to the plot with the resulting p-value. The comparisons argument specifies which groups to compare, and the method argument specifies the statistical test to use. The appearance of the significance level and stars is controlled by arguments such as size, vjust, and tip.length.

R




# Load the required packages
library(ggplot2)
library(ggpubr)
 
# Load a dataset.
data(iris)
 
# Create the box plot using `ggplot()`
# function with `aes()` mapping the x-axis
# to `Species` and y-axis to `Petal.Length`.
p <- ggplot(iris, aes(x = Species,
                      y = Petal.Length)) +
 
  # Fill the boxes with light blue color
  # and black border.
  geom_boxplot(fill = "lightblue",
               color = "black") +  
 
  # Add label for y-axis.
  ylab("Petal Length")  
 
# Add significance level and stars using
# `stat_compare_means()` function.
# Here, comparisons are made between
# "versicolor" and "setosa" and "virginica" and "versicolor" species.
p + stat_compare_means(comparisons = list(c("versicolor",
                                            "setosa"),
                                          c("virginica",
                                            "versicolor")),
                       label = "p.format",
                       method = "t.test",
                       size = 8,
                       vjust = -1.5,
                       tip.length = 0.01)


Explanation: In the above code, we first load the ggplot2 and ggpubr packages. We then load a pre-defined dataset, in this case, the iris dataset. We create a boxplot of the Petal.Length variable grouped by Species. We then use the stat_compare_means() function from the ggpubr package to add significance level and stars to the plot. The comparisons argument specifies the pairwise comparisons to be made (in this case, comparing the mean Petal.Length of versicolor to setosa, and virginica to versicolor). The method argument specifies the statistical test to be used (in this case, a t-test). The label argument specifies the format of the p-value labels. Finally, we specify the appearance of the significance level and stars using the size, vjust, and tip.length arguments.

Output:

How to Define Data When Using ggsignif With Grouped Bar Graphs and Facet_Wrap in R

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads