Create Dropdown Menu in plotly Graph in R (Example)


I am happy to welcome you to another interesting tutorial from Statistics Globe. In this one, I will show you how to add a dropdown menu to your plotly graph using the R programming language. It is really simple to do as you will see soon.

But first, let’s see a quick summary of what we will do in this tutorial:

Let’s get right into it!


Install & Load plotly

In order to use plotly, we need to first of all download and install it in our R programming environment. Therefore, in R Studio or in any other preferred IDE that can run R, please run the line of code below to download and install R:


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

Having downloaded and installed plotly, the next thing to do is to load it, so that we can have access to all of plotly’s plot-building functions:


Install & Load dplyr

We will also need to install and load the R dplyr library, which is the most popular library in R programming for data manipulation and analysis. With the dplyr library, we can make use of the pipe operator to run lines of code together as a chunk. Therefore, run the lines of code below to install and load dplyr:


Note, however, that you can still run plotly code without needing dplyr’s pipe operator. Nevertheless, piping lines of code together makes the code neater and easier to understand, and is typically considered best practice.

Build a Plotly Plot

Let’s now build a simple violin plot using the popular iris dataset for the demonstration. In R Studio, the iris dataset comes preloaded. But if you are using another IDE, then you may need to download the dataset from an online dataset repository.

You can print out the first 10 rows of the data frame if you like, as follows.

#   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#1           5.1         3.5          1.4         0.2  setosa
#2           4.9         3.0          1.4         0.2  setosa
#3           4.7         3.2          1.3         0.2  setosa
#4           4.6         3.1          1.5         0.2  setosa
#5           5.0         3.6          1.4         0.2  setosa
#6           5.4         3.9          1.7         0.4  setosa
#7           4.6         3.4          1.4         0.3  setosa
#8           5.0         3.4          1.5         0.2  setosa
#9           4.4         2.9          1.4         0.2  setosa
#10          4.9         3.1          1.5         0.1  setosa

Now we can build a violin plot of the sepal width.

fig <- iris |> 
  plot_ly(x = ~Species, 
          y = ~Sepal.Width, 
          split = ~Species,
          type = "violin")

Hovering over the plot, you will see important summary statistical information about the sepal width of each species of the iris flower. However, our goal is to add some more interactivity to our plot by including a dropdown filter, and that is what we will do next.

Add a Dropdown Menu to a plotly Plot

Here, we will include a dropdown filter to our violin plot which will enable us to switch between different kinds of visualizations. You can run the chunk of code below to add a dropdown filter to the violin plot.

fig <- iris |> 
  plot_ly(x = ~Species, 
          y = ~Sepal.Width, 
          split = ~Species,
          type = "violin") |> 
    title = "Dropdown Menu in plotly in R",
    updatemenus = list(
        buttons = list(
            method = "restyle",
            args = list("type", "violin"),
            label = "Violin Plot"
            method = "restyle",
            args = list("type", "box"),
            label = "Box Plot"
            method = "restyle",
            args = list("type", "bar"),
            label = "Bar Plot"

As you can see in the plot above, we have added a dropdown menu that enables us to switch from a violin plot to either a box plot or a bar plot.

In the layout() function, we parsed a list to the updatemenus = argument, which contains the types of plots in the dropdown filter and their respective labels. And as you have just seen, it is actually very simple to add a dropdown filter to a plotly graph in R. You can try to add other kinds of visualizations to the list, in order to increase the options available to your users.


Video, Further Resources & Summary

Do you need more explanations on how to make dropdown menu 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 in some more detail how to make dropdown menu in plotly graph in R.


The YouTube video will be added soon.


I hope you found this tutorial helpful. If yes, then be sure to check out other interesting plotly in R tutorials on Statistics Globe:

This post has shown how to add a dropdown menu in 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.