Open In App

Create Boxplot with respect to two factors using ggplot2 in R

Last Updated : 30 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Boxplots are an effective way to visualize the distribution of data, especially when comparing multiple variables. The ggplot2 package in R allows us to easily create grouped boxplots, which are helpful when we have multiple subgroups within a variable.

The function used for creating boxplots in ggplot2 is geom_boxplot(). When working with two factors, we can use the fill argument to differentiate between the groups.

Syntax

geom_boxplot(width,notch,color,size,linetype, fill,outliner.color, outliner.size, outliner.shape)

Parameter:

  • width: width of the boxplot.
  • notch: if it is true then it will create a notched boxplot and notches are used to compare boxplots.
  • color, size, linetype: Control the appearance of the boxplot’s border.
  • fill: used to fill box plot areas.
  • outlier.colour, outlier.shape, outlier.size: The color, the shape and the size for outlying points.

Installing the Requirements

To plot the boxplots , we must first install and load the ggplot2 package in R programming language.

R
install.packages("ggplot2")
library(ggplot2)

Example 1: Boxplot with a Single Factor Grouped by Another

In this example, we are creating a boxplot where Gender is plotted on the x-axis and Values on the y-axis. We are also grouping the data by the Group factor using the fill argument. We are using ggplot2 to plot a boxplot where Gender is the factor on the x-axis, and Values are plotted along the y-axis. The fill argument differentiates the groups based on the Group variable.

R
Gender<-sample(c("Male","Female"),20,replace=TRUE)
Values<-rnorm(20,mean=0,sd=1)
Group<-sample(letters[1:5],20,replace=TRUE)

df<-data.frame(Gender,Values,Group)

library(ggplot2)

ggplot(df,aes(Gender,Values))+geom_boxplot(aes(fill=Group))

 Output:

Create a boxplot with respect to two factors using ggplot2

Example 2: Boxplot with Two Factors

In this example, we are creating a boxplot based on the interaction of two factors. The two factors are Factor1 (representing gender) and Factor2 (representing age group). We then use the interaction() function to combine both factors. We are using the interaction() function to combine Factor1 and Factor2. The boxplot then shows the interaction between these two factors, with the fill argument differentiating the combinations of both factors.

R
library(ggplot2)

df <- data.frame(
  Factor1 = factor(rbinom(30, 1, 0.55), labels = c("Male", "Female")),
  Factor2 = factor(rbinom(30, 1, 0.45), labels = c("Young", "Old")),
  Values = rnorm(30, mean = 5, sd = 2)
)

df$Factor1Factor2 <- interaction(df$Factor1, df$Factor2)

ggplot(df, aes(x = Factor1Factor2, y = Values)) + 
  geom_boxplot(aes(fill = Factor1Factor2))

Output:

Create Boxplot with respect to two factors using ggplot2

Example 3: Customizing the Boxplot with Custom Colors and Jitter

In this example, we are customizing the boxplot with custom colors using scale_fill_manual(). To show how the data is distributed within the boxes, we overlay individual data points using geom_jitter(). We also customize the plot’s aesthetics by changing the width, transparency, and the shape of outliers, along with adjusting the theme and labels.

R
library(ggplot2)

df <- data.frame(
  Factor1 = factor(rbinom(30, 1, 0.55), labels = c("Male", "Female")),
  Factor2 = factor(rbinom(30, 1, 0.45), labels = c("Young", "Old")),
  Values = rnorm(30, mean = 5, sd = 2)
)

df$Factor1Factor2 <- interaction(df$Factor1, df$Factor2)

custom_colors <- c("steelblue", "darkorange", "forestgreen", "firebrick")

ggplot(df, aes(x = Factor1Factor2, y = Values, fill = Factor1Factor2)) +
  geom_boxplot(width = 0.5, alpha = 0.7, outlier.shape = NA) +
  geom_jitter(width = 0.2, height = 0, size = 3, alpha = 0.8) +
  scale_fill_manual(values = custom_colors) +
  labs(x = "Factor 1 & Factor 2", y = "Values") +
  ggtitle("Box Plot with Factor 1 & Factor 2")
  theme_minimal() +
  theme(
    plot.title = element_text(size = 16, face = "bold"),
    axis.text = element_text(size = 12),
    axis.title = element_text(size = 14, face = "bold"),
    legend.title = element_blank(),
    legend.position = "none"
  )

Output:

Create boxplot with respect to two factors using ggplot2

In this article, we learned how to create boxplots in ggplot2 for one and two factors. We are able to customize the appearance of the boxplots using different functions like geom_boxplot() and geom_jitter().



Next Article
Article Tags :

Similar Reads