R Plotly禁用图例单击和图例双击

3
我希望能够使用R Plotly从服务器端禁用图例选择。我们在这里看到,可以通过以下方式在plotly javascript上实现此目标: gd.on('plotly_legendclick',function() { return false; }) 是否有任何方法可以使用event_register()event_data()在R中实现此功能?
我发现了一个使用CSS的hacky解决方案来禁用图例。然而,如果您有多个不同的图表用于相同的output$gg,则CSS代码将禁用所有图表的图例。
Reprex:
最终目标是,单击下面的图例不得隐藏任何点。
library(shiny)
library(plotly)
library(tidyverse)

ui <- fluidPage(
  plotlyOutput("gg"),
  verbatimTextOutput("click"),
  verbatimTextOutput("doubleclick")
)

server <- function(input, output, session) {

  output$gg <- renderPlotly({
    p <- ggplot(mtcars, aes(wt, mpg, color = factor(cyl))) +
      geom_point() + 
      facet_wrap(~vs)
    ggplotly(p) %>%
      event_register("plotly_legendclick") %>%
      event_register("plotly_legenddoubleclick")
  })

  output$click <- renderPrint({
    event_data("plotly_legendclick")
  })

  output$doubleclick <- renderPrint({
    event_data("plotly_legenddoubleclick")
  })
}

shinyApp(ui,server)
2个回答

3
这是一个htmlwidgets::onRender的工作:
library(plotly)
library(htmlwidgets)

x <- c(1:15)
y <- c(1:15)
w <- gl(3,5)
dat <- data.frame(x = x, y = y, w = w)
example <- ggplot(dat, aes(x = x, y = y, color = w)) + geom_line()

ggplotly(example) %>% 
  onRender("function(el,x){el.on('plotly_legendclick', function(){ return false; })}")

2

htmlwidgets::onRender 不是必需的。我们可以简单地使用 layout()

library(plotly)

x <- c(1:15)
y <- c(1:15)
w <- gl(3, 5)
dat <- data.frame(x = x, y = y, w = w)
example <- ggplot(dat, aes(x = x, y = y, color = w)) + geom_line()

ggplotly(example) %>% layout(legend = list(
  itemclick = FALSE,
  itemdoubleclick = FALSE,
  groupclick = FALSE
))

运行 schema() 并导航至:

对象 ► 布局 ► 布局属性 ► 图例 ► 项单击

以获取有关这些参数的更多信息。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接