Hatched polygon leaflet

How to fill a hatched area polygon with holes in leaflet with R

Draw SpatialPolygons with holes using hatched area

Drawing polygons filled with a hatched background is quite easy with classical plot in R. This only requires to define density and angle parameters of polygon function. SpatialPolygons from library sp also uses this polygon function. However, if you want to draw a hatched area of a SpatialPolygons when there are holes, this may not work perfectly as the hole is filled with the plot background color to hide the hatched area of the surrounding polygon. Hence, if you want to draw a polygon with holes over a background image, you will not be able to see what is behind holes, which is a shame because this is one aim of a hole. Here, I show how you could get rid of this behaviour by using SpatialLines to draw the hatched area.
By the way, this trick is also useful for polygons in leaflet widgets as, to my knowledge, hatched polygons is not implemented. Some may want to draw hatched areas instead of coloured polygons with transparency, in particular when there is area superimposition.

A library on github

I built a R package, available on my statnmap github, to provide the function hatched.SpatialPolygons. This function is based on the plot method for SpatialPolygons of library sp. I modified the functions and sub-functions to remove all code for direct drawing and allow to output a SpatialLinesDataFrame object, which can then be drawn over any plot, with or without the original SpatialPolygons. A vignette is provided with the library, you can get the complete Rmarkdown code of the present article here.

Draw hatched areas in polygons

I created two polygons, one representing a hole inside the other and draw it with classical polygon function.

StatnMap - Hatched classic Polygons

Draw hatched areas in multiple SpatialPolygons

Let’s duplicate polygons at different positions and transform these polygons as SpatialPolygons, including holes. Using default graphical options, polygons can be plotted with hatched areas and holes are visible. Polygon drawing function uses the color of the background to fill holes, so that they appear as hole. However, if you want to superimpose your layer over another layer, holes will hide the background image.

StatnMap - Polygons with Holes and Background in R

Create SpatialLines to draw SpatialPolygons hatched areas, out of the holes

To avoid filling holes with hatched lines, I decided to use SpatialLines and crop lines that were over a hole using library rgeos.
I had to account for multiple polygons and thus created a dataframe with the SpatialLines to record original polygons ID. Thus, the number of features in the SpatialLines is not the same than the original SpatialPolygons but the ID column should allow to retrieve the correct polygon and define common colors for instance.

StatnMap - Draw Spatial Polygons with holes and hatched area

Draw hatched SpatialPolygons in leaflet

An interesting possibility of the function is that it can also be used for leaflet widgets, which, to my knowledge, lacks the possibility to fill polygons with a hatched area. (Be careful when using colornames with leaflet, they should exist in css/html, otherwise, you should use hexa code)

StatnMap - Draw hatched polygon area using leaflet and R

Draw hatched SpatialPolygons in ggplot2

Of course, this can also be used with ggplot2.

StatnMap - Multiple SpatialPolygons with hatched area

Function hatched.SpatialPolygons can be found in my R package HatchedPolygons on my statnmap github. This R package has been built only for this function