R Plotly中的下拉菜单

R Plotly中的下拉菜单,r,drop-down-menu,plotly,R,Drop Down Menu,Plotly,我正在尝试在plotly plot中添加一个下拉菜单。代码是: 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 s

我正在尝试在plotly plot中添加一个下拉菜单。代码是:

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
library(plotly)
图书馆(readxl)
Sys.setenv(“plotly_username”=“xxx”)
Sys.setenv(“plotly_api_key”=“xxx”)

复杂的东西!我找到了一个老套的解决方案,我创建了两个变量,1:0代表所有字母,2:0代表beta,0代表字母。然后,在第一个按钮列表中调用
list(“z”,list(df2$hits))
,然后调用
list(“z”,list(df2$hits2))
可以得到正确的绘图。解决方案是添加多个元素,但我似乎无法适应()复杂的东西!我找到了一个老套的解决方案,我创建了两个变量,1:0代表所有字母,2:0代表beta,0代表字母。然后,在第一个按钮列表中调用
list(“z”,list(df2$hits))
,然后调用
list(“z”,list(df2$hits2))
可以得到正确的绘图。解决方案是添加多个元素,但我似乎无法适应()