I have a leaflet map and some plotly charts on an R markdown flexdashboard.
Essentially, I would like it so that when users hovers with their mouse over a location in the map, it would act as a filter such that the graph would then automatically update based on that specific location which is being hovered over.
Assume that they're both coming from the same dataset. Here's a dummy script to potentially work with.
library(plotly)
library(leaflet)
library(tidyverse)
df <- rbind(data.frame(adm0 = c("Afghanistan", "Pakistan", "Tajikistan"),
variable = as.integer(1:3),
date = as.Date("2021-01-01")),
data.frame(adm0 = c("Afghanistan", "Pakistan", "Tajikistan"),
variable = as.integer(4:6),
date = as.Date("2021-02-01")),
data.frame(adm0 = c("Afghanistan", "Pakistan", "Tajikistan"),
variable = as.integer(7:9),
date = as.Date("2021-03-01"))) %>%
# Imagine there's some sort of administrative data that I'm binding it to
left_join(administrative_data, by = "adm0")
# Map
leaflet(df) %>%
addTiles() %>%
addProviderTiles(providers$CartoDB.DarkMatterNoLabels) %>%
setView(lat = 34, lng = 68, zoom = 5.45) %>%
# IPC layer
addPolygons(
fillOpacity = 1,
color = "white",
stroke = TRUE,
weight = 1,
group="IPC",
highlightOptions = highlightOptions(weight = 2,
color = "black"))
# Plotly
plot_ly(x = df$date,
y = df$variable) %>%
add_trace(x = df$date,
y = df$variable,
type = 'bar',
color = data$adm0)
In this dummy example, hovering over a certain country like Afghanistan in the leaflet map should then filter the plotly chart such that only Afghanistan shows up. My final output so far has optimally been via an R flexdashboard, though I'll be open to hearing if other solutions are required.
You can use GADM for administrative level boundaries (I just happen to be using a specific shapefile for corporate boundaries).
Thanks.