How can I create an htmlwidget in R that links spatial points to a line plot (i.e. - on click of a point on the map, a line plot is drawn)?
To the best of my knowledge, in R one of the only ways to link an interactive HTML map to a line plot is via leaflet
and leafletProxy()
(e.g. - this example).
Highmaps has this JS example (shown above) that links spatial polygons to a line plot, and looks adaptable to swapping the polygons for spatial points. However, within the R highcharter
package there doesn't seem to be a way to re-create this example.
Example
In the example below, the goal is to have line plot 1 appear when city 1 is clicked, and line plot 2 appear when city 2 is clicked.
I'm open to implementations of this in other R packages as well, as long as they don't require a shiny runtime.
library(highcharter)
# map dataframe
cities <- data.frame(
name = c("London", "Birmingham"),
lat = c(51.507222, 52.483056),
lon = c(-0.1275, -1.893611),
z = c(1, 2)
)
# line plot data frame
city_pop <- data.frame(time = c(1:4),
cty1 = c(3:6),
cty2 = c(6:3))
# map with points
hcmap("countries/gb/gb-all", showInLegend = FALSE) %>%
hc_add_series(data = cities, type = "mapbubble", maxSize = '10%')
# line plot for city 1
hchart(city_pop, "line", hcaes(x = time, y = cty1))
# line plot for city 2
hchart(city_pop, "line", hcaes(x = time, y = cty2))