1

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
enter image description here
And it can be found here.

Mooncrater
  • 4,146
  • 4
  • 33
  • 62
  • 1
    Complex stuff! I found a hacky solution where I created 2 variables, 1: with 0 for hits for all alphas and 2: with hits for the betas and 0 for the alphas. Than, calling `list("z",list(df2$hits))` in the first buttons list and `list("z",list(df2$hits2))` gives you the proper plot. The solution is adding multiple elements but I cannot seem to fit that (https://community.plot.ly/t/dropdown-with-multiple-attribute-changes/2005) – timfaber Oct 30 '17 at 15:53

0 Answers0