Change Size of plotly Graph in R (Example)


Hello again! Welcome to another exciting plotly in R programming tutorial from Statistics Globe. In this tutorial, I will show you how to change the size and margins of your plotly graphs in R. It is very simple to do as you shall soon see.

But first, here’s a quick overview of what we will do in this tutorial:

Great! Let’s get started!


Install & Load plotly

In order to use the R plotly library, we must first of all download and install it. Therefore, if you have not already done so, in your R Studio IDE or in any other preferred code editor that can run the R programming language, run the line of code below; otherwise, you can skip to the next section:


The above line of code will install plotly and all its dependencies in our R programming environment.

Having downloaded and installed plotly, we will need to load the library, so that we can have access to all of its plot-building functions, including the one we will make use of in this tutorial to build a line plot. Therefore, run the code below to load plotly:


Install & Load dplyr

We will also need to install and load the popular R dplyr library, which is the foremost library for data analysis and manipulation in R. It will also enable us to make use of the pipe operator to run lines of code together as one chunk, which is an easier way to run code in R. Run the code below to install and load dplyr:



Build a plotly Plot

We will make use of the cars dataset that comes preloaded in R Studio. However, if you are using another IDE, you may download the cars dataset from github. But, please, also note that you can use any other dataset of your choice in order to follow along with this tutorial.

If you would like to take a look at the first 10 rows of the cars dataset, then run the code below.

#    speed dist
#1      4    2
#2      4   10
#3      7    4
#4      7   22
#5      8   16
#6      9   10
#7     10   18
#8     10   26
#9     10   34
#10    11   17

For the illustration, we will now build a line plot plotting the speed column against the dist column.

fig <- cars |> 
  plot_ly(x = ~speed, 
          y = ~dist, 
          type = "scatter", 
          mode = "marker") |>
  layout(title = "Car Speed Vs Distance")

The plot sizes and margins are set by default in plotly. However, a programmer can manually adjust the sizes and margins of a plot, as we will see in the next section.


Adjust plotly Plot Sizes and Margins

Here, we are going to adjust the sizes and margins of our plot above and also color the margins.

m <- list(
  l = 50,
  r = 50,
  b = 50,
  t = 50
figs <- cars |> 
  plot_ly(x = ~speed, 
          y = ~dist, 
          type = "scatter", 
          mode = "marker",
          width = 600,
          height = 400) |>
  layout(title = "Car Speed Vs Distance",
         margin = m,
         paper_bgcolor = "lightblue")

As you can see, the plot size has been adjusted to a width of 600 and height of 400. Also, the plot’s left, right, bottom, and top margins have all been set to 50 and colored as well. In the plot_ly() function, we defined the width and height arguments, and in the layout() function, we parsed the m list to the margin = argument in order to adjust its margins, and we colored it light blue via the paper_bgcolor= argument.

So, that is how to adjust or change the size of plotly graphs in the R programming language. As you can see, it is very simple and easy to do.

Video, Further Resources & Summary

Do you need more explanations on how to change the size of plotly graphs 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 change the size of plotly graphs in R.


The YouTube video will be added soon.


Well, I hope you have learned something new in this tutorial. If that is true, then be sure to check out other interesting plotly in R programming tutorials on Statistics Globe:

This post has shown how to change the sizes and margins of plotly graphs in R. In case you have further questions, you may leave a comment below. I will see you soon at the next one!


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.