1

Data

I have a dataframe which contains 35,000 lat/lon locations. The locations have been plotted onto an interactive leaflet map.

The Situation

I would like to publish the map online via a markdown document.

The Problem

When I export the map as an html page or in markdown the map is:

  • Laggy
  • Hard to navigate
  • Webpage Loads slowly

Questions

  1. What is the maximum number of points you plot on a leaflet map without compromising the ability to navigate the map?

  2. Would publishing the map as a shiny application help solve the loading speed, the maps lagginess and other performance issues?

  3. If not, what other mapping programs integrate with R that yall would recommend?

Thank you for your suggestions!

Ryan P
  • 13
  • 1
  • 3

2 Answers2

2

There are a couple options I can think that might help. The best would probably be to make clusters (see Marker Clusters):

addMarkers(..., clusterOptions = markerClusterOptions())

This prevents all 35 000 points from rendering at once which speeds up the loading time.

addCircles() and addCircleMarkers() seem to load quicker than addMarkers() as well if they're okay for your purposes although they're still slow with 35 000 points.

You could then do:

addCircleMarkers(..., clusterOptions = markerClusterOptions())

which should load even faster.

Update

Use leaflet.glify (devtools::install_github("tim-salabim/leaflet.glify"))

[Now leafgl (devtools::install_github("r-spatial/leafgl")) - see comment below.]

See leaflet.glify [Now leafgl - see comment below.]

Eli Berkow
  • 2,628
  • 1
  • 12
  • 22
  • Hi! Thank you for your help! I tried that this morning but I think placing the dots into clusters minimizes the visual impact of the map. I found this example - http://shiny.rstudio.com/gallery/superzip-example.html and it appears they have 30k datapoints. However the map functions much more fluid then my own and I cannot figure out why. Maybe my styling presets are too aggresive. Who knows. – Ryan P Oct 30 '18 at 02:41
  • Alright it looks like mapping points with leaflet is just slow. The author of that example just only posted 10,000 of the 30,000 plots. https://github.com/rstudio/shiny-examples/blob/master/063-superzip-example/server.R. Bummer I will look for another mapping tool unless there is another solution. – Ryan P Oct 30 '18 at 02:48
  • Okay, Good luck! – Eli Berkow Oct 30 '18 at 08:01
  • These might help. https://gis.stackexchange.com/questions/227868/high-performance-markers-on-leaflet-map mentions a L_PREFER_CANVAS option that might be worth looking into otherwise this seems really promising https://stackoverflow.com/questions/43015854/large-dataset-of-markers-or-dots-in-leaflet. Read through the answers and comments particularly this link https://github.com/tim-salabim/leaflet.glify – Eli Berkow Oct 30 '18 at 08:12
  • I have tested leaflet.glify and it works really well. The example uses 1 000 000 points – Eli Berkow Oct 30 '18 at 08:58
  • Thanks Eli! I will give that a try after work! That is exactly what I was looking for! In case someone else is looking for another solution I also found mapbox. – Ryan P Oct 30 '18 at 13:36
  • Sure, let me know how it goes. – Eli Berkow Oct 30 '18 at 13:43
  • Were you able to export the map? I am unable to export the map in markdown, or save it as an html page. For the purpose of mapping thousands of points it does indeed work! Thank you! – Ryan P Oct 30 '18 at 21:59
  • It seems not. I usually use shiny to publish apps and it doesn't seem to render these types of maps. – Eli Berkow Oct 31 '18 at 08:16
  • Bummer. Should i start looking for a 3rd party mapping platform/program to use? Is there another langauge such as python which might having a mapping tool that can better visualize the data? Other than using leaflet I do not have that much experience in mapping. Quite frankly, im surprised other people havent encountered this problem before -- and thank you for your help! – Ryan P Oct 31 '18 at 13:50
  • I am honestly out of my depth here. Maybe ask the developer of that package on GitHub? – Eli Berkow Nov 01 '18 at 15:11
  • 1
    Hi, leaflet.glify r package maintainer here. First of all note this package is now at r-spatial/leafgl. It now has dedicated shiny functions and should also work in rmarkdown documents. If you still have issues, please post an issue at the new githib location – TimSalabim Mar 12 '19 at 19:47
1

An approach I did use recently to plot more than 100k points and worked very well:

leaflet(options = leafletOptions(preferCanvas = TRUE))

This forces leaflet to render the map as canvas. More info here.

The map appearance keep the same, but is much faster in terms of navigation. I didn't try leaflet.glify yet, but it seems to be a good solution to plot thousand points using leaflet package.

Bruno Pinheiro
  • 964
  • 8
  • 20