2

I am using the leaflet package for R to plot circles in sizes and colors that are determined by a set of variables. It works well, but not all circles can be selected with the cursor. I cannot show the mouseover tooltip or popup window for circles that are hidden underneath larger circles.

How can I fix that? I already have on/off toggles in my legend to simplify the map and hide categories, but the issue persist when overlapping circles belong to the same category.

A drag-n-drop feature, or a way to cycle through circles under the cursor using the mouse wheel would be perfect, but I am not sure this can be achieved in R.

Thank you.

[Edit] Here is an example:

library(leaflet)

n <- c(50, 100)
coord <- matrix(c(-72.3, -70.3, -72, -70), nrow=2, byrow=T)

map <- leaflet() %>%
  addTiles() %>%
  setView(lng = -72, lat = -70, zoom = 5)
  for (i in 1:length(n)) {
    map <- map %>% addCircles(lng = coord[i, 1], lat = coord[i, 2], radius = n[i]*1000, # Radius is proportional to sample size
             label=paste0("Mouseover tooltip ", i),
             popup=paste0("Popup window ", i),
             highlightOptions=highlightOptions(color="black", weight=2))
    }

map

Output with circle 2 preventing selection of circle 1 because it is on top:

enter image description here

Radius is a function of sample size in the data set, so I cannot freely manipulate it to avoid overlapping, and cannot reduce it an all circles either because I already have very small samples in the data set. Blue would be one category here (other categories/colors exist in the data set but can toggled off from the legend so it's not an issue when they hide other categories). The circle 1 is smaller than the circle 2 due to a smaller sample.

Of course, here I drew the only two circles using addCircles twice, so I could just decide to plot the smaller circle after the larger one so it's on the top layer. But in my data, there are tens of circles plotted automatically according to the variables and I cannot sort manually the order in which circles from a single category are plotted.

[Edit 2] Just found out that adding sendToBack=T to the highlightOptions=highlightOptions()line helps. It allows cycling through overlapping circles by sending those on the front to the back after they have been hovered by the cursor. However it's not perfect when there are more than 2 circles, because the user would hardly cycle voluntarily using this method, the cursor has to be brought in and out several times until the correct circle is highlighted. Better than nothing though. If anyone has another method to suggest, please don't hesitate.

lovalery
  • 4,524
  • 3
  • 14
  • 28
Blab
  • 33
  • 4
  • Please provide a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example-aka-mcve-minimal-complete-and-ver) – Heikki Nov 23 '17 at 12:35
  • I just added an example. – Blab Nov 23 '17 at 13:04
  • Just found out that adding `sendToBack=T` to the `highlightOptions=highlightOptions()`line helps. It allows cycling through overlapping circles by sending those on the front to the back after they have been hovered by the cursor. However it's not perfect when there are more than 2 circles, because the user would hardly cycle voluntarily using this method, the cursor has to be brought in and out several times until the correct circle is highlighted. Better than nothing though. If anyone has another method to suggest, please don't hesitate. – Blab Nov 24 '17 at 10:09

0 Answers0