Skip to content

Instantly share code, notes, and snippets.

@AliciaSchep
Last active February 15, 2023 10:39
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AliciaSchep/beca4cd1145b8caf7c8136e5979039b4 to your computer and use it in GitHub Desktop.
Save AliciaSchep/beca4cd1145b8caf7c8136e5979039b4 to your computer and use it in GitHub Desktop.
Use dropdown in R plotly to update both trace and axis title
## 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