[R] Leaflet maps. Nudging co-incident markers

Gavin Rudge (Institute of Applied Health Research) G.Rudge at bham.ac.uk
Fri Jan 19 18:51:48 CET 2018

I have a dataset showing points, with a category for each point and its location.

I simply want to display my points, in a way that users can toggle the points on and off by category.

Where I have two objects in the same category I'd like to display them nudged to appear as two distinct, but very close points.
I have made reproduceable example (the places are not real), which is loosely based on a tutorial I found recently (https://allthisblog.wordpress.com/2016/10/12/r-311-with-leaflet-tutorial/)

I have three categories of things (cafes, libraries and galleries), at three locations but have four objects in my set. This is because on of my locations has two functions - there is a cafe at a gallery (North St Gallery and the Gallery Cafe on the same site)    

If I make a selection that includes galleries and cafes there are just two points. I would like to nudge the point for the North St Galley and the Gallery Cafe so they appear as two (very close) points on the map and display the name when clicked on.

Also if anyone has any suggestions for generally tidying up the code I'd be grateful as my real version is much more complex with many more points and marker categories.   I believe there are Java libraries out there for managing markers and how they behave, but I'm hoping this can be done in the Leaflet R library somehow. 

The only solution I could think of was to interrogate the entire dataframe, identify points that were had the same co-ords and move them diagonally apart by adding and subtracting a fixed amount of longitude and latitude from the co-ordinates. 

Thanks in advance.


Here is the code:


#make data frame of points

x=c(-1.9116, -1.9116,-1.9237,-1.91848)
n=c('North St Gallery','Gallery cafe', 'South St Library', 'Coffee 2 go')

#get a map and zoom into approx area of interest

m=leaflet()%>% setView(lng = -1.935, lat=52.485, zoom=12)

#create groups of objects

c= subset(d,cat=="Cafe")
l= subset(d, cat=="Library")
g= subset(d, cat=="Gallery")  

#add markers
                     lng = c$x,
                     lat = c$y,
                     popup = c$n,
                     radius = 5,
                     stroke =FALSE,
                     fillOpacity = 0.75,
                     group = "1 - Cafes")
                   lng = g$x,
                   lat = g$y,
                   popup = g$n,
                   radius = 5,
                   stroke =FALSE,
                   fillOpacity = 0.75,
                   group = "2 - Galleries")

                   lng = l$x,
                   lat = l$y,
                   popup = l$n,
                   radius = 5,
                   stroke =FALSE,
                   fillOpacity = 0.75,
                   group = "3 - libraries")

m = addLayersControl(m, overlayGroups = c("1 - Cafes","2 - Galleries","3 - libraries"))


More information about the R-help mailing list