SatRday Paris: Build interactive waffle plots

Can mapping tools be diverted to other uses? Of course ! See how we play with leaflet and leafgl to quickly render a giant waffle made of millions of polygons.

Spatial tools are not only for spatial data

At SatRday in Paris, I presented a talk entitled “Everything but maps with spatial tools”.
The aim is to show that there exists powerful tools in some fields that can be applied to other fields. You just have to open your mind a little bit. Personally, I like playing with maps and spatial data. In fact, I see rasters everywhere…

In this blog post, I present a small part of my talk. It seems that whatt has left its mark on people’s minds is the waffle story… So, this is the first one I will share with you !

Waffles are electronic components

At ThinkR, we develop and deploy Shiny applications. In this case, it is for a client working with electronic components. I can not say more about this work. This is why I used this analogy with waffles.

Open your mind I said ! Use your imagination.

Imagine the world largest waffle. A waffle with millions of cells.

Brooklyn, Martha Friedman Waffle

We’re going to add some chocolate on it. However, we are not really good at it and it is not possible to put the same quantity of chocolate in each cell.

For each cell, we know the coordinates of its center. We know the quantity of chocolate. In this job, we want to be able to:

Explore each cell if needed

Zoom in/zoom out over this millions of cells

Show this waffle in a Shiny application

Switch from one waffle to another as smoothly and quickly as possible

A raster is a multipolygons structure

A package that recently appeared among spatial tools within R is {leafgl}. This is a Github only R package available here: https://github.com/r-spatial/leafgl. It allows quick rendering of millions of points using leaflet.
Waffles were the perfect opportunity to test this package !

A waffle is a regular grid. This may be seen as a raster. However, {leafgl} only works with vector objects. Of course, {leaflet} can print rasters, but this was not quick enough for us. Not always, but sometimes speed matters…
So again, open your mind and try to see a spatial vector when looking at a raster.

Do you see rasters as polygons now ?
So we’re going to play with a bigger waffle! We can, so why would we deprive ourselves of it?

Plot an heart-shaped waffle

A rectangle is boring, let’s build a heart shaped waffle with {sf}. The heart is a polygon from which we can extract a regular set of point using st_sample. This set of point symbolizes the center of each waffle cell. Then we can generate more or less chocolate in each cell.
The trick is to use spatial tools on non-spatial data. The heart we draw is not to be placed on a real position on Earth. > Indeed, I am currently writing this blog post in the train (#OnTheTrainAgain as says Colin), thus the heart drawn has no fixed position. Even if I am on Earth, my train is not following a heart shaped path. Well, you understand the idea…

Hence, to be able to play with this polygon, it is better to assign it a projection. Let’s add a Lambert93 projection (EPSG: 2154), because I am in France !

The projection chosen is however a problem. {leaflet} requires spatial objects with non-projected geographic coordinates (EPSG: 4326). Why didn’t I assign directly coordinates in degrees, then ? Because {leaflet} produces a map in Mercator projection (EPSG: 3857), and I do not want my heart to be deformed by the change in projection. Why didn’t I assign a Mercator projection then ? Because I like maps and I dislike Mercator as it gives a really bad representation of distances and surfaces of our World. But this is another problem… In our case, I will indeed have to create my polygon grid with a Mercator projection, transform it as geographic coordinates, so that my heart is not in a bad mood on the final map.
To transform a regular set of points into a polygon grid ready for leaflet while integrating deformation, I created package {waffler}, available on Github only (https://github.com/ThinkR-open/waffler). Use devtools::install_github("ThinkR-open/waffler") to try it.
We’ll use function wafflerize. The fact parameter is here to magnify the size of the heart. If we keep a resolution of 1 for a 500x500 square waffle, this will be too small to appear with distinguishable geographical coordinates.