Create Boxplot with respect to two factors using ggplot2 in R
Last Updated :
30 Apr, 2025
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()
.
Similar Reads
Create boxplot for continuous variables using ggplot2 in R
Box plots are a good way to summarize the shape of a distribution, showing its median, its mean, skewness, possible outliers, its spread, etc. Box-whisker plots are the other name of Box plots. These plots are mostly used for data exploration. The box plot is the five-number summary, which is the mi
3 min read
How to create a plot using ggplot2 with Multiple Lines in R ?
In this article, we will discuss how to create a plot using ggplot2 with multiple lines in the R programming language. Method 1: Using geom_line() function In this approach to create a ggplot with multiple lines, the user need to first install and import the ggplot2 package in the R console and then
3 min read
How To Make Boxplots with Text as Points in R using ggplot2?
In this article, we will discuss how to make boxplots with text as points using the ggplot2 package in the R Programming language. A box plot is a chart that shows data from a five-number summary including one of the measures of central tendency. These five summary numbers are Minimum, First Quartil
3 min read
Draw ggplot2 Barplot With Round Corners in R
In this article, we will be looking at the various approaches to drawing ggplot2 barplot with round corners in the R programming language. Method 1: Draw ggplot2 Barplot with Round Corners Using ggchicklet Package In this approach to drawing a ggplot2 barplot with round corners, the user needs to fi
2 min read
How to reorder barplots with facetting with ggplot2 in R?
In this article, we will discuss how to reorder bar plots with facetting using the ggplot2 package in the R Programming Language. We can draw a facet bar plot by using the geom_col() function with the facet_wrap() function of the ggplot2 package. Syntax: ggplot( dataframe, aes( x, y ) ) + geom_col()
2 min read
How To Reorder Boxplots in R with ggplot2?
In this article, we will discuss how to reorder the boxplot with ggplot2 in R Programming Language. To reorder the boxplot we will use reorder() function of ggplot2. Syntax: ggplot(sample_data, aes(x=reorder(name,value),y=value)) By default, ggplot2 orders the groups in alphabetical order. But for b
2 min read
How to create a pie chart with percentage labels using ggplot2 in R ?
In this article, we are going to see how to create a pie chart with percentage labels using ggplot2 in R Programming Language. Packages Used The dplyr package in R programming can be used to perform data manipulations and statistics. The package can be downloaded and installed using the following co
4 min read
Create Boxplot of Multiple Column Values using ggplot2 in R
In this article, we will discuss how to create a boxplot of multiple column values using ggplot2 in R Programming Language. A dataframe can be created by containing values organized in the form of rows and columns. The values may belong to different data types. The reshape2 package is used to aggreg
2 min read
How to plot means inside boxplot using ggplot2 in R?
In this article, we are going to see how to plot means inside boxplot using ggplot in R programming language. A box plot in base R is used to summarise the distribution of a continuous variable. It can also be used to display the mean of each group. Means or medians can also be computed using a boxp
4 min read
How To Show Mean Value in Boxplots with ggplot2?
In this article, we will discuss how to show mean value in Boxplot with ggplot2 using R programming language. Firstly, we will create a basic boxplot using the geom_boxplot() function of the ggplot2 package and then do the needful, so that the difference is apparent. Syntax: ggplot() + geom_boxplot(
2 min read