trevorld / ggpattern

ggplot geoms with pattern fills
https://trevorldavis.com/R/ggpattern/dev/
Other
361 stars 18 forks source link

Alpha masks exhausted if using cairo device with R 4.1 masks enabled #59

Closed trevorld closed 2 years ago

trevorld commented 2 years ago

Bug description

When drawing detailed maps with geom_sf_pattern() in a Cairo graphics device with R 4.1 clipping enabled one may see

Warning in .setClipPath(path$f, path$ref) :
  Cairo masks exhausted

and the patterns are not properly clipped.

Minimal, reproducible example

library(ggplot2)
library(sf)
library(rnaturalearth)
library(ggpattern)

shapefile <- ne_countries(continent = 'europe', returnclass = "sf", scale = 110)

png("masking_bug.png", type = "cairo")
ggplot(shapefile) +
  geom_sf_pattern(aes(geometry = geometry, fill = name, pattern_fill = name),
                  pattern = "stripe") +
  xlim(-10, 30) +
  ylim( 37, 60) +
  theme_bw() +
  theme(legend.position = 'none')
dev.off()

clipping_bug

Workarounds

1) Use ragg::agg_png() or grDevices::pdf() instead

2) Set options(ggpattern_use_R4.1_masks = FALSE) to use the raster masking alternative to the R 4.1 masking feature

3) Upgrade to R 4.2.0 (r-devel) where this limit on alpha masks should be lifted.

fixed_with_option

Possible future fixes

If we could easily figure which {sf} shape are polygons without holes and which are we could use a normal pattern grob for the ones without holes instead of always using a clipping path grob and this would put less clipping pressure on Cairo and hopefully reduce such warnings.

trevorld commented 2 years ago

Prof. Murrell claims that the limit on masks on Cairo devices should be lifted in r-devel.

trevorld commented 2 years ago

I've manually confirmed that the limit on masks on Cairo devices seems to be lifted in R-devel so this bug should go away after users upgrade to R 4.2.