Open In App

Boxplots in R Language

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

A boxplot (also known as a box-and-whisker plot) is used to visualize the distribution of data based on five key statistics (minimum, first quartile (Q1), median, third quartile (Q3), and maximum). They also show outliers and provide a visual representation of how data is spread.

It displays the distribution of a dataset through its summary, which includes:

  1. Minimum: The smallest data point, excluding outliers.
  2. First Quartile (Q1): The median of the lower half of the dataset.
  3. Median: The middle value of the dataset.
  4. Third Quartile (Q3): The median of the upper half of the dataset.
  5. Maximum: The largest data point, excluding outliers.

In this article, we'll explore how to create boxplots in R, customize them, and visualize multiple datasets simultaneously.

Creating a Boxplot in R

Boxplots are created in R by using the boxplot() function.

Syntax:

boxplot(x, data, notch, varwidth, names, main)

Parameters: 

  • x: This parameter sets as a vector or a formula.
  • data: This parameter sets the data frame.
  • notch: This parameter is the label for horizontal axis.
  • varwidth: This parameter is a logical value. Set as true to draw width of the box proportionate to the sample size.
  • main: This parameter is the title of the chart.
  • names: This parameter are the group labels that will be showed under each boxplot.

Implementation of Box Plots in R

We will create box plots using an in built dataset in R programming language.

1. Importing Dataset 

We use the data set "mtcars" , which is available in R language and explore the columns: "mpg" and "cyl".

R
data(mtcars)
head(mtcars)

2. Creating the Boxplot

We will create the boxplot for the relationship between displacement and gear:

R
boxplot(disp ~ gear, data = mtcars,
        main = "Displacement by Gear",
        xlab = "Gear",
        ylab = "Displacement")

Output: 

Box plot in RGeeksforgeeks
Box plot in R

Boxplot using notch

A notch in a boxplot helps you compare the medians of different groups by adding a confidence interval around the median. The notch is useful when comparing multiple boxplots to see if their medians differ significantly.

1. Set up Custom Colors

We'll first define custom colors for the boxplot:

R
my_colors <- c("#FFA500", "#008000", "#1E90FF", "#FF1493")

2. Create the Boxplot with Notch

We'll create the boxplot with the notch. In the arguments of the boxplot function:

  • col: Customizes the box colors.
  • border: Sets the box borders to black.
  • notch: Adds a notch to the box for visual comparison of medians.
  • notchwidth: Controls the width of the notch.
  • medcol: Colors the median line white.
  • whiskcol: Colors the whiskers black.
  • boxwex: Adjusts the width of the boxes.
  • outpch: Specifies the shape of outliers (solid circles).
  • outcol: Colors the outliers black.
R
boxplot(disp ~ gear, data = mtcars,
        main = "Displacement by Gear", xlab = "Gear", ylab = "Displacement",
        col = my_colors, border = "black", notch = TRUE, notchwidth = 0.5,
        medcol = "white", whiskcol = "black", boxwex = 0.5, outpch = 19,
        outcol = "black")

Output:

Box plot in RGeeksforgeeks
Box Plot in R

Multiple Boxplot

Sometimes, we want to create multiple boxplots for several variables. In this example, we’ll plot boxplots for multiple variables in the mtcars dataset (example: mpg, disp, hp, wt).

1. Define Variables for Boxplots

We define a list of variables for which we want to create boxplots:

R
variables <- c("mpg", "disp", "hp", "wt")

2. Set up Plotting Layout

We will set up the layout for multiple plots. This will arrange the boxplots in a single row, with as many columns as there are variables

R
par(mfrow = c(1, length(variables)))

3. Create Boxplots for Each Variable

We will create the boxplots using a loop. Where:

  • get(var): Dynamically selects the variable for plotting from the dataset.
  • The loop generates a boxplot for each variable specified in the variables list.
  • par(mfrow = c(1, length(variables))): Arranges the plots in a single row.
R
par(mfrow = c(1, length(variables)))

for (var in variables) {
  boxplot(get(var) ~ gear, data = mtcars,
          main = paste("Box Plot of", var),
          xlab = "Gear",
          ylab = var,
          col = "skyblue",
          border = "black",
          notch = TRUE,
          notchwidth = 0.5,
          medcol = "white",
          whiskcol = "black",
          boxwex = 0.5,
          outpch = 19,
          outcol = "black")
}
# Reset the plotting layout
par(mfrow = c(1, 1))

Output:

Multiple box plots in RGeeksforgeeks
Multiple box plots in R

In this article, we've explored how to create basic and customized boxplots in R using the boxplot() function. We also saw how to add notches to compare medians and how to visualize multiple boxplots simultaneously.


Next Article
Article Tags :
Practice Tags :

Similar Reads