'Pattern in ggpattern doesn't follow polygon borders after update

I made some maps with spatial features from the sf package and used ggpattern to create a striped fill. This worked well for certain polygons, but not for those with holes in it (islands in a lake feature class).

Recently the ggpattern package has been updated to work with polygons with holes (for which I am grateful!), and it no longer gives errors for these features but now the pattern fill is wrong for all features (with and without holes), since it no longer follows the polygon borders but instead fills a rectangle around the polygons.

Here's an example using the rnaturalearth package to show what I mean.

#devtools::install_github("coolbutuseless/ggpattern")
library(ggpattern)
library(rnaturalearth)

germany <- ne_countries(country='germany', returnclass='sf')
ggplot() + geom_sf_pattern(data = germany)

This gives me this as a result.

this

And here are pictures of my actual map before and after the ggpattern update for polygons without holes, using unchanged code and data.

I've looked through the ggpattern page to see if I am missing certain parameters that I need to change, but can't seem to find the issue. I am relatively new to R so maybe I overlooked something. Would appreciate any help to point me in the right direction!



Solution 1:[1]

I got the same issue. If you go on Github it's bug already reported. However, the solution proposed doesn't work for me. So I propose you an alternative solution.

devtools::install_github("statnmap/HatchedPolygons")
require(HatchedPolygons)
require(rnaturalearth)
    
ger <- st_as_sf(germany)

hatch <- hatched.SpatialPolygons(ger, density = 4, angle = 45)
hatch <- hatch %>% sf::st_set_crs(st_crs(germany))

ggplot(germany) + 
  geom_sf() +
  geom_sf(data = hatch)

enter image description here

If you want specific hatched regions, you should first create the hatched polygon desired and then clip it to your data and finally you plot it as sf object.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Arnaud Louchart