I have a toy example below where I have a scatterplot that stays on the same x and y coordinates but I'd like to color the scatter points by the values in different columns. When I click on "Petal.Width" the correct scatter point colors appear but the colorbar disappears. Ideally I'd like the title of the color bar to be the same as the clicked button and the scale of the color bar to change with each clicked button.
updatemenus <- list(
list(
active = -1,
type= 'buttons',
buttons = list(
list(
label = "Petal.Length",
method = "update",
args = list(list(visible = c(FALSE, TRUE)))),
list(
label = "Petal.Width",
method = "update",
args = list(list(visible = c(TRUE, FALSE))))
)
)
)
iris %>%
plot_ly(type = "scatter",
mode = 'markers') %>%
add_trace(x = ~Sepal.Length,
y = ~Sepal.Width,
color = ~Petal.Length,
visible = TRUE,
name = "Petal.Length") %>%
add_trace(x = ~Sepal.Length,
y = ~Sepal.Width,
color = ~Petal.Width,
visible = FALSE,
name = "Petal.Width") %>%
layout(updatemenus=updatemenus)
Here is how the plot is displayed when i run my code
Here it is when I click "Petal.Width"
I will answer in case someone finds this issue in the future:
What worked for me is specifying some arguments for colorbar
in both traces ( add_trace
).
You would end with something like this:
iris %>%
plot_ly(type = "scatter",
mode = 'markers') %>%
add_trace(x = ~Sepal.Length,
y = ~Sepal.Width,
color = ~Petal.Length,
visible = TRUE,
name = "Petal.Length",
colorbar= list(thicknes=20,
len = 0.35,
x=0.05,
y=0.35)
) %>%
add_trace(x = ~Sepal.Length,
y = ~Sepal.Width,
color = ~Petal.Width,
visible = FALSE,
name = "Petal.Width",
colorbar= list(thicknes=20,
len = 0.35,
x=0.05,
y=0.35)
) %>%
layout(updatemenus=updatemenus)
You can further customize your colorbar with the arguments found in the Plotly documentation for R
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.