I'm trying to add a drop down menu in this plotly plot. The code is :
library(plotly)
library(readxl)
Sys.setenv("plotly_username"="xxx")
Sys.setenv("plotly_api_key"="xxx")
df <- read_excel("From\\a\\certain\\location.xlsx")
k = (df$keyword=="alpha")
j = (df$keyword=="beta")
# give state boundaries a white border
l <- list(color = toRGB("grey"), width = 0.5)
# specify some map projection/options
g <- list(
showframe = FALSE,
showcoastlines = FALSE,
projection = list(type='Mercator')
)
df2 = df
p <- plot_geo(df2, locationmode = 'country names') %>%
add_trace(
z = ~hits, locations = ~location,
color = ~hits, colors = 'Blues',marker=list(line=l)
) %>%
colorbar(title = "Hits") %>%
layout(
title = 'For Addiction',
geo = g
)
p <-p %>% layout(
updatemenus = list(
list(
buttons = list(
list(method = "restyle",
args = list("df2",list(df[k,])),
label = "alpha"),
list(method = "restyle",
args = list("df2",list(df[j,])),
label = "beta")))
))
# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
chart_link = plotly_POST(p, filename="Example.html",sharing = c("public"))
chart_link
The plot, does contain the drop down menu, but it doesn't work. Changing the values of the drop down doesn't change the plot itself.
There is a similar question, but following it's (only) answer, I didn't get the drop down to work.(by putting the df[k,]
and df[j,]
in lists. So how to make those drop downs work? Any help appreciated.
The example dataset is
And it can be found here.