Custom Button in plotly Graph in R (Example)

 

Hello! You are welcome to another exciting plotly in R programming tutorial. In this tutorial, I will show you how to create custom buttons in your plotly visualization in R, such that you can switch between graphs at the click of a button. You will find that it is very simple and easy to do.

But before we get started, here is a quick overview of this tutorial:

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

 

Install & Load plotly

If you do not have plotly already installed in your R environment, then in either R Studio or in any other code editor of your choice that can run R, run the line of code below to download and install plotly; otherwise, you can skip to the next section:

install.packages("plotly")

With plotly now downloaded and installed, the next thing to do is to load the library and all its dependencies in our R environment. Therefore, run the code below to do so:

library(plotly)

Install & Load dplyr

We will also need to install the R dplyr library, which enables us to use the pipe operator to pipe lines of code together and run them as one chunk. Piping code together has the advantage of making code easier to read and understand, and is usually considered best practice. Therefore, in your IDE, run the lines of code below to install and load dplyr:

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

 

Create Custom Buttons in plotly Graph

Now that we have successfully installed and loaded both plotly and dplyr, let us now build our interactive plotly graph, and incorporate buttons inside the graph. We will make use of the popular iris dataset in this project. The iris dataset comes preloaded in R Studio, but it can also be downloaded from an online dataset repository, such as the UCI Machine Learning Repository. However, you can use any dataset of your choice to follow along.

We will visualize the sepal length of the different species of the iris flower, and create buttons that will enable a user to switch between a violin plot, a box plot, and a bar plot. Run the chunk of code below:

fig <- iris |> 
  plot_ly(x = ~Species, 
          y = ~Sepal.Length, 
          split = ~Species,
          type = "violin") |> 
  layout(
    title = "Custom Buttons in plotly in R",
    updatemenus = list(
      list(
        type = "buttons",
        direction = "bottom",
        pad = list("r" = 0, "t" = 10, "b" = 10),
        buttons = list(
          list(
            method = "restyle",
            args = list("type","violin"),
            label = "Violin Plot"
          ),
          list(
            method = "restyle",
            args = list("type","box"),
            label = "Box Plot"
          ),
          list(
            method = "restyle",
            args = list("type","bar"),
            label = "Bar Plot"
          )
        )
      )
    )
  )
 
fig


We have created a plotly graph object that enables a user to switch between different ways of visualizing the data at the click of a button. To achieve this, in the layout() function, we parsed a list to the updatemenus = argument, wherein we defined the type of widget that we wanted to create (“buttons”), the direction that the buttons should face (“bottom”), and the padding of the buttons (“r”, “t”, “b”). Another list was parsed to the buttons = argument to detail the kinds of visualizations and their respective labels.

Other buttons can be created to add some more interactivity to the graph, such as to change the colors of the plots or to change their layout, but those are beyond the scope of this tutorial. Nevertheless, you can explore these possibilities as well.

 

Video, Further Resources & Summary

Do you need more explanations on how to create custom buttons in 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 how to create custom buttons in plotly graph in R.

 

The YouTube video will be added soon.

 

With that, I hope you have learned how to create custom buttons in plotly graphs in R. If you would like to learn more about using plotly in the R programming language, then be sure to check out other interesting plotly in R tutorials on Statistics Globe:

This post has shown how to create custom buttons in plotly graph in R. In case you have further questions, you may leave a comment below. I will see you 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.

Menu
Top