3

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)?

enter image description here

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))
Rich Pauloo
  • 7,734
  • 4
  • 37
  • 69
  • 1
    Hi. This is the simplest example of what you are looking for in pure JS in Highcharts: https://jsfiddle.net/BlackLabel/2my43n7g I am not R developer, but I should be able to rewrite this code to R (using JS("") function). The only thing that stops me is that I don't know how to display two charts in different, separate containers in R. – raf18seb Oct 07 '19 at 09:58
  • Maybe the crosstalk package can help you ? http://rstudio.github.io/crosstalk/ – fmarm Oct 10 '19 at 22:56
  • Excellent idea @fmarm, and I enjoyed looking through the docs. Unfortunately, {crosstalk} only supports linked brushing, which is far less intuitive than clicking on a leaflet marker. – Rich Pauloo Oct 11 '19 at 01:06

0 Answers0