自定义情节图和传单地图之间的突出显示

我的目标是在一个面板中的绘图图形和第二个面板中的地图之间链接突出显示。出于我的目的,我想在一个面板中选择(突出显示)数据,并在两个面板中以红色突出显示所选数据,而不是使未选择的数据变暗。无论使用哪个面板来选择数据,这都应该有效。在下面,我有两种方法,两种方法都突出显示两个面板之间的数据,但突出显示颜色和调光存在功能问题。

方法A:在这种方法中,我用传单映射以避免mapbox。我也在使用,mode ='markers+lines'因为在我的真实数据中,数据点沿 x 轴的间距非常接近,并且将数据与线连接起来在视觉上很有帮助,尽管这会引入我在代码下面描述的功能问题。

library(plotly)
library(dplyr)
library(leaflet)
library(htmlwidgets)
library(crosstalk)

set.seed(1)
dat <- tibble(name = letters[1:21]
              , latitude = seq(38.6,38.8,0.01)
              , longitude = seq(-86.4,-86.8,-0.02)
              , var.x = 1:21
              , var.y = runif(21))

dat.a <- highlight_key(dat)

p <- plot_ly(dat.a) %>%
  add_trace(x = ~var.x, y = ~var.y
            , text = ~name
            , alpha = 0.8
            , mode = 'markers+lines', type = 'scatter'
            , color = I("#1f77b4")
            , showlegend = FALSE) %>%
  highlight(on = "plotly_selected"
            , off = "plotly_deselect"
            , opacityDim = 1
            , color='red') 

map <- leaflet(dat.a) %>%    
  addTiles() %>%
  # addProviderTiles(providers$CartoDB.Positron) %>%
  addCircles(lat = ~latitude
             , lng = ~longitude
             , stroke = TRUE
             , color = I("#1f77b4")
             , weight = 3
             , opacity = .8) %>%
  highlight(on="plotly_selected")

bscols(widths = c(5, 7)
       , p, map)

方法 A 评论:在下面的屏幕截图中,我从图中(左面板)中选择了 4 个数据点,其中 y<=0.2。问题 1:选择了地图(右侧面板)中的正确点,但在突出显示的点(地图中)中保持蓝色,未选择的点变暗。问题 2:选择后,图(左面板)在点之间添加一条红线,如 var.x= 5 和 var.x=10。(问题 2 的解决方案只涉及突出显示符号而不是图中的线条(左面板)是可以接受的)。

方法 B:在这种方法中,我使用 plotly 进行映射,这很好,因为地图中具有更高级的选择功能

library(plotly)
library(dplyr)
library(htmlwidgets)
library(crosstalk)

set.seed(1)
dat <- tibble(name = letters[1:21]
              , latitude = seq(38.6,38.8,0.01)
              , longitude = seq(-86.4,-86.8,-0.02)
              , var.x = 1:21
              , var.y = runif(21))

dat.a <- highlight_key(dat)

p <- plot_ly(dat.a) %>%
  add_trace(x = ~var.x, y = ~var.y
            , type = "scatter"
            , text = ~name
            , alpha = 0.8
            , mode = 'lines+markers'
            , color = I("#1f77b4")
            , showlegend = FALSE) %>%
  highlight(on = "plotly_selected"
            , off = "plotly_deselect"
            , opacityDim = 1
            , color = 'red')

map <- plot_ly(dat.a) %>% 
  add_trace(lon = ~longitude, lat = ~latitude
          , type = "scattermapbox"
          , text = ~name
          , alpha = 0.8
          , mode = "marker"
          , color = I("#1f77b4")
          , hoverinfo = ~name) %>%
  # Plot the data again but using the original data
  add_trace(lon = ~longitude, lat = ~latitude
            , data = dat
            , type = "scattermapbox"
            , text = ~name
            , alpha = 0.8
            , mode = "markers"
            , color = I("#1f77b4")
            , showlegend = FALSE) %>%
  layout(
    mapbox = list(
      style = 'open-street-map',
      zoom = 8.5,
      center = list(lon = mean(dat$longitude), 
                    lat = mean(dat$latitude)))) %>%
  highlight(on = "plotly_selected"
            , off = "plotly_deselect"
            , color = 'red')

bscols(p, map, widths = c(5, 7))

方法 B 注释:运行代码时,从图中选择数据(左面板)正确突出显示地图中的数据(右面板)——这似乎重复工作。问题 1:在最初运行代码时,第一次从地图(右面板)中选择数据正确突出显示了图中(左面板)中的数据。从地图结果中进行的后续选择不会获得所需的红色,并且还会使未选择的点变暗。问题 2(与方法 A 问题 2 相同):选择后,图(左面板)在点之间添加一条红线,如 var.x= 5 和 var.x=10。(问题 2 的解决方案只涉及突出显示符号而不是图中的线条(左面板)是可以接受的)。

以上是自定义情节图和传单地图之间的突出显示的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>