Layout & Style of plotly Graph in R (Example)

 

Hello and welcome to another interesting tutorial where I will show you how you can style your plotly graph using the R programming language. In this tutorial, we will build a simple bar plot, after which we will customize its appearance.

Here is a quick preview of what we will do in this project:

Let’s now dive into it!

 

Install R plotly Package

If you have not already installed the R plotly package, then run the line of code below in R Studio or in any other preferred code editor that can run the R programming language.

But if you have already installed plotly, then you may skip to the next section:

install.packages("plotly")

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

Load R plotly Library

Next, you want to load the plotly library. To do so, run:

library(plotly)

This will load plotly and all its plot-building functions in your R programming environment.

It is also necessary to install the R dplyr package as well, which is the go-to library in R for data analysis and manipulation. The dplyr library also enables R programmers to pipe lines of code together and run them as a chunk. This practicality will be needed while using the plotly library. Therefore, run the lines of code below to install and load the R dplyr library:

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

 

Build a Simple Bar Plot

We will now create a simple data frame of students and their respective ages. Run the code below to create the data frame:

student_ages <- data.frame(
  students = c("Harry","Jerome","Lisa","Tony","Anne","Jessica"),
  ages = c(25,40,19,34,27,45)
)

You can print the data frame by running:

print(student_ages)
#   students ages
#1    Harry   25
#2   Jerome   40
#3     Lisa   19
#4     Tony   34
#5     Anne   27
#6  Jessica   45

Let us now build a bar plot to visualize the ages by people:

fig <- student_ages |> 
  plot_ly(x = ~students, y = ~ages, color = ~students, type = "bar") |> 
  layout(title = "A Simple Bar Plot")
fig


Nice! We have just built a simple bar plot. As you can see, it is a very basic plotly bar plot. There is nothing unique about its appearance, and that is something we are going to change in the next section.
 

Style the Bar Plot

We are now going to change the layout and style of the same bar plot. We will change the plot’s background color, grid color and bar color, and set their border width. We will also change the color and font family of the plot title, as well as the colors of the X and Y axis titles and labels. Finally, we will give a legend title and color. Seems like a lot, but it is very simple to implement these customizations as you will see in the code below:

fig <- student_ages |> 
  plot_ly(x = ~students,
          y = ~ages,
          color = ~students,
          type = "bar",
          marker = list(color = c("yellow",
                                  "red",
                                  "blue",
                                  "purple",
                                  "orange",
                                  "pink"),
                        line = list(color = "black",
                                    width = 4))) |> 
  layout(title = list(text = "<b>A Simple Bar Plot</b>",
                      font = list(color = "yellow",
                                  family = "Sitka Small")),
         plot_bgcolor = "gray",
         paper_bgcolor = "black",
         xaxis = list(color = "blue",
                      title = "Students",
                      gridcolor = "black"),
         yaxis = list(color = "green",
                      title = "Ages",
                      gridcolor = "black"),
         legend = list(
           title = list(
             text = "<b>Students</b>",
             font = list(color = "purple")
           )
         ))
fig


Great! Our bar plot looks considerably different from what it looked like when we first created it. In our code above, we used the marker = argument inside the plot_ly() function to set the bar colors and the border width of the bars. Then in the layout() function, we used a basic html tag b to make our plot title bold. Then we colored it yellow and set its font family to Sitka Small. You can choose any other font type, such as Times New Roman or Arial.

In the layout() function, we defined the plot_bgcolor = argument as gray and the paper_bgcolor = argument as black, and also customized the X and Y plot axes and grid colors too. Finally, we gave the legend a title and used the html b tag to make it bold and colored it purple.

Our plot may not be the prettiest or nicest looking plot, but we have demonstrated all of these styling options for the sake of this tutorial, to see what is possible in plotly in terms of appearance customization.

There are many other plotly customizations, all of which we cannot possibly cover in this tutorial. Nevertheless, you can play around with these parameters and style your plot in the way you want, and also in a way that communicates the information to the end user best.

So that is how to customize the layout and style of your plotly visualization using the R programming language. If you learned something new in this tutorial, then be sure to check out other R plotly tutorials on Statistics Globe.

I will see you soon in the next one!

 

Video, Further Resources & Summary

Do you need more explanations on how to customize the layout and style of a plotly graph 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 customize the layout and style of plotly graphs 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 customize the layout and style of plotly graphs 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