Last active
February 15, 2023 10:39
-
-
Save AliciaSchep/beca4cd1145b8caf7c8136e5979039b4 to your computer and use it in GitHub Desktop.
Use dropdown in R plotly to update both trace and axis title
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Makes plot of either mpg or hp versus wt using mtcars dataset | |
## Dropdown enables toggling between what variable to plot on y axis | |
library(plotly) | |
plot_ly(mtcars, x = ~wt) %>% | |
add_markers(y = ~mpg, name = "mpg") %>% | |
add_markers(y = ~hp, name = "hp", visible = FALSE) %>% | |
layout( | |
title = "Drop down menus - Update", | |
xaxis = list(domain = c(0.1, 1)), | |
yaxis = list(title = "mpg"), | |
showlegend = FALSE, | |
updatemenus = list( | |
list( | |
y = 0.7, | |
buttons = list( | |
list(method = "update", | |
args = list(list(visible = list(TRUE, FALSE)), | |
list(yaxis = list(title = "mpg"))), | |
label = "mpg"), | |
list(method = "update", | |
args = list(list(visible = list(FALSE, TRUE)), | |
list(yaxis = list(title = "hp"))), | |
label = "hp"))) | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment