串扰和情节 - 在悬停时仅显示单点,隐藏所有其他点

时间:2018-04-15 19:25:28

标签: r shiny plotly

我一直在玩剧情和串音,并设法将地图和图表链接在一起,但我似乎无法获得我想要的格式。

我想:

  1. 映射以始终显示所有标记
  2. 将鼠标悬停在地图标记上时,     仅显示图表中的单个点。目前突出显示一点,其他点可见但褪色。褪色点将被删除。
  3. 根据标记
  4. 更新图表轴

    到目前为止,我有以下代码作为概念证明:

        ---
        title: crosstalk_quake_test
        output: html_document
        --- 
    
        ```{r}
        library(crosstalk)
        library(plotly)  #Must use devtools::install_github("ropensci/plotly")
    
        Sys.setenv('MAPBOX_TOKEN' = '{insert your token here}')
    
        # Wrap data frame in SharedData
        sd <- SharedData$new(head(quakes,100))
    
        # Note - plot_mapbox does not seem to render map within RStudio but works fine in Chrome browser 
        bscols(
         plot_mapbox(sd, lat = ~lat , lon = ~long, mode = 'scattermapbox') %>% 
          layout(mapbox = list(zoom = 3,
                           center = list(lat = -22 ,lon = 179)),
           showlegend=FALSE) %>%
         highlight(on = 'plotly_hover'),
        plot_ly(data = sd, x = ~depth, y = ~mag, type = 'scatter', mode = 'markers',
           marker = list(size = 15))%>% 
          layout(showlegend = FALSE)
        )
        ```
    

    我是否需要使用闪亮的,或者这可以用剧情和串扰完成吗?

    最终游戏类似于:https://dash-oil-and-gas.plot.ly/

    请注意地图和图表是否正确。

2 个答案:

答案 0 :(得分:0)

如果您使用opacityDim = 0参数plotly::highlight,您将获得所有积分,但其中一个积分将消失。也就是说,使用此代码:

bscols(
 plot_mapbox(sd, lat = ~lat , lon = ~long, mode = 'scattermapbox') %>% 
  layout(mapbox = list(zoom = 3,
                   center = list(lat = -22 ,lon = 179)),
   showlegend=FALSE) %>%
   highlight(on = 'plotly_hover'),
 plot_ly(data = sd, x = ~depth, y = ~mag, type = 'scatter', mode = 'markers',
   marker = list(size = 15))%>% 
   highlight(opacityDim = 0) %>% 
   layout(showlegend = FALSE)

我不确定你想要的第3点;显示单个点的图表不会有多大用处。如果只有一个点存在,你真的想更新轴吗?

如果这是你想要的,我不知道你是否可以用plotly来做。 crosstalk并未使用{{1}},因此有关如何处理它的约定尚未确定,但在某些情况下,当其他控件指定过滤操作时,您会获得所需的行为突出显示操作。

答案 1 :(得分:0)

我似乎无法获得plotly / crosstalk选项来完全按照我的意愿行事。  我最终得到了plotly / shiny解决方案...我不想说它不能用plotly / crosstalk来完成

使用此示例,在图表中仅绘制一个点,并且轴会相应更新

library(plotly) ##Must use devtools::install_github("ropensci/plotly")
library(shiny)

ui <- fluidPage(
  fluidRow(
    column(6, plotlyOutput(outputId = "map")),
    column(6, plotlyOutput(outputId = "chart"))),
  verbatimTextOutput("hover")
)

server <- function(input, output){

  Sys.setenv('MAPBOX_TOKEN' = '{insert your token here}')

  quake_df <- (head(quakes,100))

  # dateframe must have an ID to link on, not needed if df already has an id
  quake_df$id <- row.names(quake_df)

  # add map
  # Note - plot_mapbox does not seem to render map within RStudio but works fine in Chrome browser
  # Note - 'key = ~id'  this will be used to filter chart data 
  output$map <- renderPlotly({
    plot_mapbox(quake_df, lat = ~lat , lon = ~long, key = ~id, mode = 'scattermapbox') %>% 
      layout(mapbox = list(zoom = 3, center = list(lat = -22 ,lon = 179)), showlegend=FALSE)
  })

  # add chart
  output$chart <- renderPlotly({  

    # set hover event
    eventdata <- event_data("plotly_hover")

    # sub set of data using hover event 
    sd_sub = quake_df[quake_df$id == eventdata$key, ]  

    plot_ly(data = sd_sub, x = ~depth, y = ~mag, type = 'scatter', mode = 'markers',
            marker = list(size = 15))

  })

  # display hover output
  output$hover <- renderPrint({
    event_data("plotly_hover")
  })

}

shinyApp(ui = ui, server = server)