Customize Legend of plotly Graph in R (Example)

 

Hello once again! I am happy to welcome you to another interesting tutorial on Statistics Globe. In this one, I will show you how to customize the legend of your plotly graph using the R programming language.

But before we start coding, let us get a quick overview of what we shall be doing in this tutorial:

With that, let’s get into it!

 

Install R plotly Library

To install the plotly library, in your R Studio IDE or any other preferred code editor that can run the R programming environment, please run the code below

install.packages("plotly")

This line of code will install plotly in your R programming environment.
 

Load R plotly Library

Next, we need to load the R plotly library. Therefore, run the line of code below to do just that:

library(plotly)

Plotly will be loaded and all its dependencies too, which means we can now use all of its plot-building functions in our project.

You will also need to install and load the R dplyr library, which will make it possible to run lines of code as a chunk, using the pipe operator. Therefore, run the lines of code below to install and load dplyr:

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

 

Build plotly Bar Plot

Now, we are going to build a simple bar plot that includes a plot legend. We will create our own data frame of different fruits and their quantities:

fruits <- data.frame(
  fruits = c("Oranges","Mangoes","Pineapples","Watermelons","Apples","Peaches"),
  quantity = c(100,85,97,120,70,110)
)

You can view the data frame by running

print(fruits)
#       fruits   quantity
#1     Oranges      100
#2      Mangos       85
#3  Pineapples       97
#4 Watermelons      120
#5      Apples       70
#6     Peaches      110

Let’s now build a plotly bar plot with a legend to visualize the data frame above. Kindly run the code chunk below to build a bar plot:

fig <-fruits |> 
  plot_ly(x = ~fruits,y = ~quantity,color = ~fruits,type = "bar") |> 
  layout(title = "A Fruity Bar Plot")
fig


In the above bar plot, we see the plot legend on the right-hand side of our visualization, and you can toggle each item in the legend box.

 

Customize Legend

Now, let us customize the look of our legend box, which is the objective of this tutorial. We are going to add a legend title, set the font family, size, and color, then color the background of the legend box, thicken the border of the box, change the legend orientation, and adjust the legend position.

We will first create a list containing all of these customizations, and then apply the list to the bar plot:

l <- list(
  font = list(
    family = "sans-serif",
    size = 10,
    color = "green"),
  bgcolor = "#E2E2E2",
  bordercolor = "black",
  x = 0, y = 1,
  orientation = "h",
  borderwidth = 3,
  title=list(text='<b> Fruits </b>'))
 
fig <-fruits |> 
  plot_ly(x = ~fruits,y = ~quantity,color = ~fruits,type = "bar") |> 
  layout(title = "A Fruity Bar Plot",
         legend = l)
fig


In the plot above, we set the font family to sans-serif, set the font color to green, and also used the html b tag to make the legend title bold. We also changed the legend box orientation from vertical to horizontal and used the x and y coordinates to reposition the box.

There are still many customizations that can be done to a plotly legend, but we have touched on just a few of these in this tutorial.

Now, you can enjoy customizing the legend of your plotly graph going forward with what you have learned in this tutorial. Therefore, I hope you liked this tutorial, and I can’t wait to see you in the next one!
 

Video, Further Resources & Summary

Do you need more explanations on how to customize the legend of your plotly graph using the R programming language? 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 legend of your plotly graph using R.

 

 

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

This post has shown how to customize the legend of your plotly plot using the R programming language. 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