Create Multiple Graphs as plotly Subplots in R (3 Examples)

 

Hello again! Welcome to another interesting tutorial from the stable of Statistics Globe. In this tutorial, you will learn how to create multiple graphs as plotly subplots in the R programming language. It is very easy to do, as you will soon see.

But first, here is a quick overview of this tutorial:

If you are ready, then let’s dive into it!

 

Install plotly Library

If you do not have plotly already installed in your Rstudio or in your preferred code editor that can run R, run the line of code below; otherwise, you may skip to the next section:

install.packages("plotly")

This will install plotly and all its dependencies in your R programming environment.
 

Load plotly Library

To load the R plotly library, run the following line of code:

library(plotly)

Loading the R plotly library will ensure that we have access to all of plotly’s plot-building functions.

We will also need to install and load the R dplyr library, which is the go-to library in R for data analysis and manipulation. With the help of dplyr, we can use the pipe operator to pipe lines of code together and run them as a chunk. Therefore, run the following lines of code to install and load dplyr:

install.packages("dplyr")
library(dplyr)

 

Create Data Frames

Let us now create the 4 data frames that we will use to demonstrate how to make multiple subplots in plotly using the R programming language. The first data frame comprises animals and their populations. This will be stored in dfA:

dfA <- data.frame(
  animals = c("dogs","cats","goats","pigs","hamster"),
  population = c(50,65,35,40,70)
)

You can print it by running:

print(dfA)
#   animals population
#1     dogs         50
#2     cats         65
#3    goats         35
#4     pigs         40
#5 hamsters         70

The second data frame comprises data about animals and their ages. We shall save it as dfB:

dfB <- data.frame(
  animals = c("dogs","cats","goats","pigs","hamsters"),
  age = c(5,10,15,8,3)
)

As we did with dfA, you can also print out dfB if you wish to do so.

The third data frame comprises animals and their weights. We shall store it in dfC:

dfC <- data.frame(
  animals = c("dogs","cats","goats","pigs","hamsters"),
  weight = c(35,20,15,40,10)
)

Finally, the fourth data frame, which comprises animals and their speed, will be named dfD:

dfD <- data.frame(
  animals = c("dogs","cats","goats","pigs","hamsters"),
  speed = c(45,50,30,15,30)
)

 

Example 1: Build Two-Column Subplots

Great! We are now ready to build a two-column subplot in plotly. We will first create the plotly figure for each data frame, and then plot them side by side. For the demonstration, we will create bar plots in this tutorial.

Please run the code below to do that:

fig1 <- dfA |> 
  plot_ly(x = ~animals,
          y = ~population,
          type = "bar",
          marker = list(color = "red"))
 
fig2 <- dfB |> 
  plot_ly(x = ~animals,
          y = ~age,
          type = "bar",
          marker = list(color = "blue"))

Next, we will use the plotly subplot() function to plot both figures side by side and the layout() function to give a plot title:

fig <- subplot(fig1,fig2) |> 
  layout(title = "Side By Side Plots")
 
fig



 

As you can see, the plots are located next to each other. Now let’s build something a bit more advanced than that!

Example 2: Build Stacked Subplots

In this section, we will build stacked subplots. First, we will create an additional plotly figure named fig3.

fig1 <- dfA |> 
  plot_ly(x = ~animals,
          y = ~population,
          type = "bar",
          marker = list(color = "red"))
 
fig2 <- dfB |> 
  plot_ly(x = ~animals,
          y = ~age,
          type = "bar",
          marker = list(color = "blue"))
 
fig3 <- dfC |> 
  plot_ly(x = ~animals,
          y = ~weight,
          type = "bar",
          marker = list(color = "green"))

Then, as we did before, we will use the subplot() function to plot the figures on top of each other:

fig <- subplot(fig1,fig2,fig3,nrows = 3) |> 
  layout(title = "Stacked Subplots")
 
fig


In the subplot() function, we used the nrow = argument to specify that we want 3 rows for our stacked plot, and then used the layout() function to give a plot title. Very easy!
 

Example 3: Build Multiplot Subplots

Let us now build multiple graphs as plotly subplots. We will make use of all 4 data frames created previously. We will create a fourth plotly graph figure, and then plot it with the others all together. The principle remains the same as with the previous examples:

fig1 <- dfA |> 
  plot_ly(x = ~animals,
          y = ~population,
          type = "bar",
          marker = list(color = "red"))
 
fig2 <- dfB |> 
  plot_ly(x = ~animals,
          y = ~age,
          type = "bar",
          marker = list(color = "blue"))
 
fig3 <- dfC |> 
  plot_ly(x = ~animals,
          y = ~weight,
          type = "bar",
          marker = list(color = "green")) 
 
fig4 <- dfD |> 
  plot_ly(x = ~animals,
          y = ~speed,
          type = "bar",
          marker = list(color = "purple"))
 
 
fig <- subplot(fig1,fig2,fig3,fig4,nrows = 2) |> 
  layout(title = "Multiple Subplots")
 
fig


In the subplot() function above, we specified that we wanted 2 rows. Since there were 4 plotly graph figures, it created 2 columns to accommodate all four plotly figures automatically. Super easy, isn’t it?

This is how to create multiple graphs as plotly subplots in the R programming language. You can try out your new plotly skills on other kinds of plots and use your creativity to impress your end users.

I hope you have learned something new by reading this tutorial. If yes, then be sure to check out other worthwhile plotly in R tutorials on Statistics Globe, and I will see you soon in the next one!

 

Video, Further Resources & Summary

Do you need more explanations on how to create multiple graphs as plotly subplots in R? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain in some more detail how to create multiple graphs as plotly subplots in R.

 

The YouTube video will be added soon.

 

Furthermore, you could have a look at some other tutorials on Statistics Globe:

This post has shown how to create multiple graphs as plotly subplots in R. In case you have further questions, you may leave a comment below.

 

R & Python Expert Ifeanyi Idiaye

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top