Draw Multiple Boxplots in One Graph in R Side-by-Side (4 Examples)
In this tutorial you’ll learn how to plot several boxplots side-by-side in the same graphic in the R programming language.
The article will contain these content blocks:
Let’s start right away!
Creation of Example Data
First, we’ll need to create some data that we can use in the following examples:
set.seed(75829547) # Create example data data <- data.frame(A = rnorm(1000), B = runif(1000), C = rpois(1000, 3)) head(data) # Head of example data # A B C # 1 0.53802755 0.23042233 3 # 2 -0.81263292 0.03925386 3 # 3 0.15503948 0.37912312 3 # 4 0.73903916 0.51420032 3 # 5 -0.07919366 0.01956273 2 # 6 -1.56211181 0.92004033 2
The previously shown output of the RStudio console shows the structure of our example data – It consists of three numeric columns A, B, and C. Each of these variables should be drawn as separate boxplot in the same graphic window in R.
Example 1: Drawing Multiple Boxplots Using Base R Graphics
In Example 1, I’ll illustrate how to use the basic installation of the R programming language to plot several boxplots in the same graph. For this, we simply need to insert the name of our data frame into the boxplot function:
boxplot(data) # Applying boxplot function
As shown in Figure 1, we created a plot showing each of our variables as different boxplot with the previous syntax.
The previous R syntax is very simple. However, the output looks not really pretty yet. In the following examples I’ll therefore explain how to create more advanced boxplot graphics with the ggplot2 and lattice packages in R.
If you want to learn more about improving Base R boxplot graphics, you may have a look here.
Example 2: Drawing Multiple Boxplots Using ggplot2 Package
In Example 2, I’ll show how to use the functions of the ggplot2 package to create a graphic consisting of multiple boxplots.
If we want to apply the functions of the reshape2 package, we first have to install and load reshape2:
install.packages("reshape2") # Install reshape2 package library("reshape2") # Load reshape2
Now, we can convert our data to long format using the melt function provided by the reshape2 package:
data_long <- melt(data) # Reshaping data frame head(data_long) # Head of reshaped data frame # variable value # 1 A 0.53802755 # 2 A -0.81263292 # 3 A 0.15503948 # 4 A 0.73903916 # 5 A -0.07919366 # 6 A -1.56211181
If we want to draw boxplots with the ggplot2 package, we also need to install and load ggplot2:
install.packages("ggplot2") # Install ggplot2 package library("ggplot2") # Load ggplot2
Finally, we can draw all boxplots to a ggplot2 graphic:
ggplot(data_long, aes(x = variable, y = value)) + # Applying ggplot function geom_boxplot()
The output of the previously shown code is illustrated in Figure 2: A ggplot2 graph containing multiple boxplots side-by-side.
Example 3: Drawing Multiple Boxplots Using lattice Package
Another popular package for drawing boxplots is the lattice package.
If we want to use the functions of the lattice package, we first need to install and load lattice:
install.packages("lattice") # Install lattice package library("lattice") # Load lattice package
Now, we can apply the bwplot function to draw our boxplots. Note that we are using the long data frame that we have created in the previous example:
bwplot(value ~ variable, data_long) # Applying bwplot function
As illustrated in Figure 3, we created a graphic with multiple boxplots with the previous code.
Example 4: Drawing Multiple Boxplots for Each Group Side-by-Side
So far, we have drawn only one boxplot for each variable of our example data. However, it is possible to add another layer by drawing multiple boxplots for each group of a variable.
Let’s do this in R!
First, we need some example data:
data(iris) # Loading iris flower data set head(iris) # Head of iris data # Sepal.Length Sepal.Width Petal.Length Petal.Width Species # 1 5.1 3.5 1.4 0.2 setosa # 2 4.9 3.0 1.4 0.2 setosa # 3 4.7 3.2 1.3 0.2 setosa # 4 4.6 3.1 1.5 0.2 setosa # 5 5.0 3.6 1.4 0.2 setosa # 6 5.4 3.9 1.7 0.4 setosa
As you can see based on the previous output of the RStudio console, our example data is the popular iris flower data set which consists of four numeric variables and the Species grouping variable.
I’m going to use the ggplot2 package in this example. For that reason, we need to reshape the iris data frame from wide to long format:
iris_long <- melt(iris, id = "Species") # Reshaping iris data head(iris_long) # Head of reshaped iris data # variable value # 1 A 0.53802755 # 2 A -0.81263292 # 3 A 0.15503948 # 4 A 0.73903916 # 5 A -0.07919366 # 6 A -1.56211181
Now, we can draw our ggplot2 boxplot graph as shown below. Note that we are specifying the color argument to be equal to our grouping column Species:
ggplot(iris_long, aes(x = variable, y = value, color = Species)) + # ggplot function geom_boxplot()
As shown in Figure 4, the previous R syntax created a graphic that shows a boxplot for each factor level group of each variable of our data frame.
Video, Further Resources & Summary
Do you want to know more about boxplots in R? Then you may watch the following video of my YouTube channel. I explain the content of this tutorial in the video.
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
In addition, you might want to have a look at the related articles on this homepage. A selection of interesting posts about graphics in R can be found below.
- Draw Multiple Overlaid Histograms with ggplot2 Package
- Draw Multiple Graphs & Lines in Same Plot
- Draw Multiple lattice Plots in One Window
- Graphics in R
- Introduction to R
In summary: You learned in this article how to create a graph containing multiple boxplots with one or multiple factor labels in the R programming language. If you have additional questions, tell me about it in the comments section below.
Statistics Globe Newsletter