trevorld / ggpattern

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

Support holed patterns in 'geom_sf_pattern()' #44

Closed trevorld closed 2 years ago

trevorld commented 3 years ago

Would be very helpful to have a minimal, clear reproducible example for

trevorld commented 3 years ago

A geom_sf() example taken from one of @coolbutuseless's other packages:

suppressPackageStartupMessages({
  library(dplyr)
  library(sf)
  library(ggplot2)
  library(sfheaders)
})

coords <- readr::read_csv(
'x,y,idx
0,0,1
4,0,1
4,4,1
0,4,1
0,0,1
1,1,2
2,1,2
2,2,2
1,2,2
1,1,2
', show_col_types = FALSE)

this_sf <- coords %>%
  sfheaders::sf_polygon(x='x', y='y', linestring_id = 'idx')

ggplot(this_sf) +
  geom_sf(aes(geometry = geometry), fill = 'lightblue')

Using grid.force() and grid.ls() it looks like geom_sf() (and geom_sf_pattern()) plots a pathGrob() object so we theoretically should be able to clip the associated pattern using gridpattern::clippingPathGrob():

1) Calculate pathGrob() 2) Calculate bounding box (via grobPoints()?) 3) Generate pattern using bounding box rectangle as boundary (so raster patterns are scaled appropriately) 4) Clip pattern to pathGrob() via gridpattern::clippingPathGrob() 5) Ideally if geom only needs polygons (instead of paths) use those directly...

trevorld commented 3 years ago