This article will demonstrate how to use the boxplot function in R to create boxplots. And it is a fundamental graph that is usually used in data analytics. So, follow below to learn how to use this function.
What is the boxplot in R?
In R, the boxplot function creates a boxplot to display the distribution of a continuous or categorical variable. The boxplots consist of a summary of the data set containing the minimum, first quartile, median, third quartile, and maximum.
And here, we will learn about the syntax of this function.
Boxplot Function in R: The Syntax
boxplot(x, data, names, main, col)
Parameters
- x: a formula or a vector of values or a matrix.
- data: a dataframe, a list, or an environment (or an object that can be coerced to one of these).
- names: a character vector giving the names of the plots.
- col: a vector of colors to be used for the boxes in the plot.
How to use the boxplot function in R?
Create a basic boxplot
First, we will create data to create a boxplot as follows:
# Create a data set.seed(100) x <- rnorm(500) # View six elements in the data head(x)
Output
[1] -0.50219235 0.13153117 -0.07891709 0.88678481 0.11697127 0.31863009
We will apply the boxplot function above to create a boxplot with this data. See the code example below
# Create a data set.seed(100) x <- rnorm(500) # Create a boxplot boxplot(x)
Output
The col argument in the boxplot function can be used to fill the boxes in the plot with a specific color. For example:
# Create a data set.seed(100) x <- rnorm(500) # Create a boxplot boxplot(x, col = 'green')
Output
You can also change the border color of the entire boxplot and add the title and names of the x-axis and y-axis in this graph. Check out the code example below:
# Create a data set.seed(100) x <- rnorm(500) # Create a boxplot boxplot(x, col = "green", border = 2, main = "The boxplot", xlab = "X-axis", ylab = "Y-axis" )
Output
Create multiple boxplots
To create multiple boxplots in R, you can pass multiple vectors or formulas to the boxplot() function. For example:
# Create a data set.seed(100) data1 <- rnorm(300) data2 <- rnorm(100) data3 <- rnorm(500) # Create multiple boxplots boxplot(data1, data2, data3)
Output
Similarly, we can customize this graph as follows:
# Create a data set.seed(100) data1 <- rnorm(300) data2 <- rnorm(100) data3 <- rnorm(500) # Create multiple boxplots boxplot(data1, data2, data3, main = "The multiple boxplot", xlab = "X-axis", ylab = "Y-axis", col = c("green", "red", "blue") )
Output
Summary
To summarize, the boxplot() function in R is used to create a box plot, a graphical representation of the distribution of a continuous or categorical variable. We hope this article is helpful for your work and you can understand by using this function.
If you have any questions, don’t hesitate to comment below.
Have a great day!

Hi, guys! My name’s Scott Miller. My current job is a software developer and I have shared a lot of quality articles about Javascript, C, C++, C#, Python, PHP, R, Java programming languages. I’m hoping they can assist you.
Name of the university: HCMUS
Major: IT
Programming Languages: C, C++, Python, R, Java, JavaScript