r-如何使用串扰小部件过滤plot_mapbox()中的点



我正试图使用mapbox(plotly::plot_mapbox()(在绘图中创建地图可视化,其中地图中的点属于不同的地理区域("comunas"(,用户可以在crosstalk::filter_select()创建的下拉窗口小部件中选择地理区域。

预期的行为是,当在小部件中选择地理区域时,它应该触发地图中的过滤器,该过滤器应该只显示属于所选区域的点。

但是,我无法使其工作(我正在按照此处提供的说明进行操作(。我写的代码成功地创建了1(地图和2(过滤小部件,但组件之间没有交互/对话,即当我在下拉列表中选择一个地区时,地图中没有过滤效果,它会不断显示所有点,而不仅仅是属于所选地区的点。

我做错什么了吗?我如何才能使这项工作按预期进行?

以下是一个具有原始数据子集的可复制示例(需要Mapbox令牌才能运行(:

library(tidyverse)
library(plotly)
library(crosstalk)
data <- 
tibble::tribble(
~Comuna,         ~Latitude,        ~Longitude, ~id,
"Alhué", -34.0227777777778, -71.0994444444444,  1L,
"Alhué", -34.0958333333333,          -71.1825,  2L,
"Algarrobo", -33.3827777777778, -71.4163888888889,  1L,
"Algarrobo", -33.3827777777778, -71.4163888888889,  2L,
"Alhué", -34.0227777777778, -71.0994444444444,  3L,
"Alto Biobío", -37.8830555555556, -71.6280555555555,  1L,
"Alto Biobío", -37.8830555555556, -71.6280555555555,  2L,
"Alhué", -33.4430555555556, -70.7488888888889,  4L,
"Alhué",           -33.445,          -70.6475,  5L,
"Alhué",           -33.445,          -70.6475,  6L,
"Algarrobo", -33.4136111111111, -71.4827777777778,  3L,
"Alto Biobío", -37.8866666666667,            -71.63,  3L,
"Alto Biobío", -38.0883333333333, -71.3458333333333,  4L,
"Alto Biobío", -38.0913888888889, -71.4113888888889,  5L,
"Algarrobo", -33.3827777777778, -71.4163888888889,  4L,
"Algarrobo", -33.3827777777778, -71.4163888888889,  5L
)
plot_example <- 
data %>%
plot_mapbox(
lat = ~ Latitude,
lon = ~ Longitude,
text = ~ Comuna,
mode = "markers",
hoverinfo = "text") %>%  
layout(mapbox = list(
style = "dark",
center = list(
lat = ~ median(Latitude),
lon = ~ median(Longitude)
),
zoom = 6
)) %>% 
group_by(Comuna)
shared_key_cells <- highlight_key(data, ~Comuna)
bscols(
filter_select("Comuna", "Comuna", shared_key_cells, ~Comuna,
multiple = FALSE),
plot_example
)

您的plot_example需要使用共享数据。这应该有效:

library(tidyverse)
library(plotly)
library(crosstalk)
data <- 
tibble::tribble(
~Comuna,         ~Latitude,        ~Longitude, ~id,
"Alhué", -34.0227777777778, -71.0994444444444,  1L,
"Alhué", -34.0958333333333,          -71.1825,  2L,
"Algarrobo", -33.3827777777778, -71.4163888888889,  1L,
"Algarrobo", -33.3827777777778, -71.4163888888889,  2L,
"Alhué", -34.0227777777778, -71.0994444444444,  3L,
"Alto Biobío", -37.8830555555556, -71.6280555555555,  1L,
"Alto Biobío", -37.8830555555556, -71.6280555555555,  2L,
"Alhué", -33.4430555555556, -70.7488888888889,  4L,
"Alhué",           -33.445,          -70.6475,  5L,
"Alhué",           -33.445,          -70.6475,  6L,
"Algarrobo", -33.4136111111111, -71.4827777777778,  3L,
"Alto Biobío", -37.8866666666667,            -71.63,  3L,
"Alto Biobío", -38.0883333333333, -71.3458333333333,  4L,
"Alto Biobío", -38.0913888888889, -71.4113888888889,  5L,
"Algarrobo", -33.3827777777778, -71.4163888888889,  4L,
"Algarrobo", -33.3827777777778, -71.4163888888889,  5L
)
shared_key_cells <- highlight_key(data, ~Comuna)
plot_example <- 
shared_key_cells %>%
plot_mapbox(
lat = ~ Latitude,
lon = ~ Longitude,
text = ~ Comuna,
mode = "markers",
hoverinfo = "text") %>%  
layout(mapbox = list(
style = "dark",
center = list(
lat = ~ median(Latitude),
lon = ~ median(Longitude)
),
zoom = 6
)) %>% 
group_by(Comuna)

bscols(
filter_select("Comuna", "Comuna", shared_key_cells, ~Comuna,
multiple = FALSE),
plot_example
)

相关内容

  • 没有找到相关文章

最新更新