如何使用串扰小部件过滤 plot_mapbox() 中的点?
How to filter points in plot_mapbox() using crosstalk widget?
我正在尝试使用 mapbox (plotly::plot_mapbox()
) 在 plotly 中创建地图可视化,其中地图中的点属于不同的地理区域(“comunas”),用户可以选择一个地理区域在 crosstalk::filter_select()
.
创建的下拉小部件中
预期的行为是,当一个地理区域在小部件中 selected 时,它应该在地图中触发一个过滤器,它应该只显示属于 select 的点ed区.
但是,我无法让它工作(我正在按照提供的说明进行操作 here)。我成功编写的代码创建了 1) 地图和 2) 过滤小部件,但这些组件不会 interact/talk 彼此,即当我 select 下拉列表中的一个区域时,那里地图上没有过滤效果,它一直显示所有的点,而不仅仅是那些属于selected区的点。
我是不是做错了什么?我怎样才能使这项工作按预期进行?
下面是一个带有原始数据子集的可重现示例(它需要一个 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
)
我正在尝试使用 mapbox (plotly::plot_mapbox()
) 在 plotly 中创建地图可视化,其中地图中的点属于不同的地理区域(“comunas”),用户可以选择一个地理区域在 crosstalk::filter_select()
.
预期的行为是,当一个地理区域在小部件中 selected 时,它应该在地图中触发一个过滤器,它应该只显示属于 select 的点ed区.
但是,我无法让它工作(我正在按照提供的说明进行操作 here)。我成功编写的代码创建了 1) 地图和 2) 过滤小部件,但这些组件不会 interact/talk 彼此,即当我 select 下拉列表中的一个区域时,那里地图上没有过滤效果,它一直显示所有的点,而不仅仅是那些属于selected区的点。
我是不是做错了什么?我怎样才能使这项工作按预期进行?
下面是一个带有原始数据子集的可重现示例(它需要一个 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
)