0

I'm trying to create a map visualization in plotly using mapbox (plotly::plot_mapbox()), where the points in the map belong to different geographical districts ("comunas") and the user can choose a geographical district in a drop-down widget created by crosstalk::filter_select().

The expected behavior is that, when a geographical district is selected in the widget, it should trigger a filter in the map, which should only show the points that belong to the selected district.

However, I'm unable to make it work (I'm following the instructions presented here). The code that I wrote successfully creates 1) the map and 2) the filtering widget, but the components don't interact/talk to each other, i.e. when I select a district in the drop-down, there is no filtering effect in the map, it keeps showing all the points, not only those who belong to the selected district.

Am I doing something wrong? How can I make this work as intended?

Below is a reproducible example with a subset of the original data (it requires a Mapbox token to run):

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
)

1 Answers1

1

Your plot_example needs to use the shared data. This should work:

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
)
Dhiraj
  • 1,650
  • 1
  • 18
  • 44