Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing marker clustering by zoom level in Shiny / Leaflet / R

Tags:

r

leaflet

shiny

I want to create a web application where a user inputs a set of identifiers, and a set of dots on a map related to those identifiers appear.

Trouble is, this data can number in the tens to hundreds of thousands to even potentially millions. Given this possibility, I want to take a light tack. Below is my ideal aggregation behavior.

At low zoom levels, I want to aggregate these dots into counts by state (appropriate symbology size/color indicating higher intensity, with the dot centered on the state centroid). at slightly higher zoom levels, these would separate into counts by smaller polygons. at even higher zoom, yet smaller polygons. When, if unaggregated, the number of dots on the map would be less than ~500, then just plot the dots.

These polygons were already decided, and each dot has within its data the polygon ID for each polygon it is under.

Since the dots are randomly plotted within respective polygons to begin with, actual distribution of dots within polygons doesn't matter. Or rather, any aggregation that ignores which polygon the dots were plotted in will wipe out information. For this reason, I can't use markercluster (at least not with the options I saw. If there's an easy means to aggregate in the manner i'm looking for, please let me know)

For various reasons (I'm not a javascript programmer, I'm an R programmer) I am working within the bounds of the leaflet package within R. Is there a means by which I can change the aggregation level depending on zoom in this manner?

I've put together a toy dataset containing a relatively small subset (1 entity, 3 states, ~10k observations) along with the centroids for census tracts and counties for those states.

http://s000.tinyupload.com/index.php?file_id=00048836337627834343

like image 486
Faydey Avatar asked Nov 08 '15 07:11

Faydey


1 Answers

This is not exactly an answer but it would hardly fit in a comment.

You would like to show aggregated data (counts of dots / markers?) per predefined geographic areas, with those areas merging at lower zoom levels (let's say it could be per cities, then counties, States, etc.).

Even in JavaScript I am not aware of any out-of-the-box solution for this use case. There are some possible workarounds, but I am not sure they would be easily do-able in R.

A usual workaround is to get rid of any automatic clustering, and generate your own markers at each zoom level, with appropriate data aggregation algorithm (not very difficult to do since you can rely on parent polygon ID). Then simply add/remove markers when zoom changes.

Unfortunately this means you will not get the animations and coverage display from MarkerCluster.

You might also be interested in this thread [Question] Build clusters depending on some geographical entity #521 from Leaflet.markercluster issues page on GitHub. The use case is somehow similar, and you can see some examples of implementations, but it did not output in any general and reliable solution unfortunately.

like image 77
ghybs Avatar answered Nov 02 '22 04:11

ghybs