Gender | 10th Marks | 12th Marks | College Marks |
---|---|---|---|
Male | 79.0 | 65 | 80 |
Female | 70.0 | 80 | 70 |
Male | 69.5 | 61 | 55 |
Interactive Plotting: Adding Dropdowns with Plotly in R
Introduction:
Data visualization is essential for understanding data, and Plotly is a popular tool in R for creating interactive plots. While Shiny is more commonly used for interactivity, Plotly offers a simpler way to add dropdown menus directly to plots.
In this blog post, we will explore how to add dropdown menus to plots using Plotly in R, with the help of two examples.
Data Snapshot:
As seen below, data contains gender of some students, and their marks from 10th grade, 12th grade and college.
Import required libraries:
We will be using dplyr
for data manipulation and plotly
for plotting graphs in this post.
library(dplyr)
library(plotly)
Example 1. Adding dropdown for selection of different plot types:
Let us start by examining a box plot that visualizes the distribution of 12th grade marks, split by gender. We will add a dropdown menu that allows users to switch between a box plot and a violin plot.
First step here is to create a boxplot as one usually does using plotly, add plot titles and axis labels. Next, using updatemenus(), we can add a dropdown menu to the plot.
<- plot_ly(df, y = ~`12th Marks`, split = ~Gender, type = "box") %>%
plot layout(
title = "Marks in 12th Grade", xaxis = list(title = ""), yaxis = list(title = "12th Marks"),
updatemenus = list(
list(
y = 0.8, # specifies the vertical position of the menu
buttons = list( # specifies list of options in dropdown menu
list(method = "restyle", # specifies action to be taken, which is 'restyled' in this case
args = list("type", "box"), # specifies the type of plot to be generated
label = "Box Plot"), # specifies option label in menu
list(method = "restyle", args = list("type", "violin"), label = "Violin Plot")
)
)
) )
The dropdown menu created contains two options: “Box Plot” and “Violin Plot”. When a user clicks one of the options, the plot is restyled accordingly.
Example 2: Adding dropdowns for changing variable to be displayed in the plot:
Next, let us consider a bar chart that displays the average marks for different variables grouped by gender. We will add a dropdown menu that allows users to switch between average marks for 10th grade, 12th grade, and college.
In order to make this bar chart, we will have to create a dataset with the aggregated marks from the main data.
<- df %>% group_by(Gender) %>%
grouped summarise(
`Avg 10th Marks` = round(mean(`10th Marks`, na.rm = T), 2),
`Avg 12th Marks` = round(mean(`12th Marks`, na.rm = T), 2),
`Avg College Marks` = round(mean(`College Marks`, na.rm = T), 2)
)
Gender | Avg 10th Marks | Avg 12th Marks | Avg College Marks |
---|---|---|---|
Female | 78.97 | 71.72 | 76.87 |
Male | 75.76 | 67.29 | 67.52 |
Then, like before, we will first create a bar chart by specifying variables on x and y axes. We will first consider ‘Avg 10th Marks’ for the main bar chart, and subsequently add traces for ‘Avg 12th Marks’ and ‘Avg College Marks’, and set their visibility to FALSE. Then, using updatemenus(), we can add the dropdown menu for switching between the three.
<- plot_ly(grouped, x = ~Gender, y = ~`Avg 10th Marks`, type = 'bar', name = 'Avg 10th Marks') %>%
plot add_trace(y = ~`Avg 12th Marks`, name = 'Avg 12th Marks', visible = F) %>%
add_trace(y = ~`Avg College Marks`, name = 'Avg College Marks', visible = F) %>%
layout(
title = "Bar Chart for Average Marks", xaxis = list(title = "Gender"), yaxis = list(title = "Marks"),
updatemenus = list(
list(
y = 0.7,
buttons = list(
list(method = "restyle",
args = list("visible", list(TRUE, FALSE, FALSE)), # controls the visibility of the trace
label = "Avg 10th Marks"),
list(method = "restyle", args = list("visible", list(FALSE, TRUE, FALSE)), label = "Avg 12th Marks"),
list(method = "restyle", args = list("visible", list(FALSE, FALSE, TRUE)), label = "Avg College Marks")
)
)
) )
A dropdown menu is added here to toggle visibility of each trace. Each button in the dropdown menu controls the visibility of a specific trace, and clicking on it hides the other two.
Note: Following video contains plots for both Example 1 and Example 2.
Conclusion:
In R, Plotly’s dropdown menus make it simple to explore data and switch between different types of plots. It’s easier to use and share than Shiny, works well with other platforms, and performs better, making it a great choice for interactive visualizations that need to be shared and perform smoothly.