'Choropleth with dropdown menu to change data. Traces not showing
i am trying to produce a choropleth map in which the user can use a dropdown menu to change between different variables using plotly in R. When doing something similar with a line chart, I could set the visibility of the second trace to FALSE and use a button to change the visibility of the different traces, as shown in the code below. However, when using plot_geo(), the plot only changes the color, the data and hoverinfo does not change. Also, the color switches back to the first trace as soon as the slider gets used.
If the user selects "stat2", I want the plot to only show the data, color and hoverinfo for this variable.
Thanks in advance!
df <- data.frame(year=c(rep(2000,5), rep(2001,5), rep(2002,5), rep(2003,5), rep(2004,5), rep(2005,5)),
stat1=sample(1:10, 30, replace=T),
stat2=sample(1:100, 30),
location=rep(c("DEU", "BEL", "FRA", "ITA", "SWE", "FIN"),5))
y=1,# position the button
buttons = list(
list(method="update",
args=list(list(visible=list(T,F))),
label="stat1"),
list(method="update",
args=list(list(visible=list(F,T))),
label="stat2")))
plot_geo(df, frame=~year) %>%
add_trace(locations=~location, z=~stat1, color=~stat1, colorscale="Blues") %>%
add_trace(locations=~location, z=~stat1, color=~stat2, colorscale="Greys", visible=F) %>%
layout(geo=list(scope="europe"),
updatemenus=list(button))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
