trevorld / ggpattern

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

Support polygons with holes #26

Closed eliocamp closed 2 years ago

eliocamp commented 3 years ago

I want to use ggpatern to hatch areas of a field with values greater (or lower) than some reference value using contours. However, this doesn't work well because of the lack of support for polygons with holes.

This would be a killer feature for me. Will it be implemented in the near future? If you know where in the code it should be implemented, Id' be happy to give it a try.

eliocamp commented 3 years ago

Chainging this line

https://github.com/coolbutuseless/ggpattern/blob/390e13fead028ba240eae9293a5ef422df02bc8e/R/geom-polygon.R#L121

to

first_idx <- !duplicated(interaction(munched$group, munched$subgroup))

and this https://github.com/coolbutuseless/ggpattern/blob/390e13fead028ba240eae9293a5ef422df02bc8e/R/geom-polygon.R#L128

to

polygons <- split(munched, interaction(munched$group, munched$subgroup))

Seems to work a lot better in my example case. image

To this:

image

trevorld commented 3 years ago
trevorld commented 2 years ago

Minimal reproducible example with holes:

      # example modified from {ggplot2} unit tests
      tbl <- data.frame(
        x = c(
          0, 10, 10, 0,
          20, 30, 30, 20,
          22, 28, 28, 22
        ),
        y = c(
          0, 0, 10, 10,
          20, 20, 30, 30,
          22, 22, 28, 28
        ),
        group = c(rep(1, 4), rep(2, 8)),
        subgroup = c(rep(1, 8), rep(2, 4))
      )

      p <- ggplot(tbl, aes(x, y, group = group, subgroup = subgroup)) +
        geom_polygon_pattern(fill="lightblue", color="black")