trevorld / ggpattern

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

Allow `pattern_fill` (and more) to take a **list** of (color) vectors #100

Closed trevorld closed 5 months ago

trevorld commented 1 year ago
library("ggplot2")
library("ggpattern")
df <- data.frame(level = c("a", "b", "c", 'd'), outcome = c(2.3, 1.9, 3.2, 1))
ggplot(df) +
  geom_col_pattern(
    aes(level, outcome, fill = level), 
    pattern = 'stripe',
    pattern_fill    = list('white', 'white', c('white', 'grey'), 'grey'),
    colour  = 'black'
  ) +
  theme_bw(18) +
  theme(legend.position = 'none') + 
  labs(
    title    = "ggpattern::geom_col_pattern()",
    subtitle = "pattern = 'stripe'"
  ) +
  coord_fixed(ratio = 1/2)
Error in grDevices::col2rgb(colour, alpha = TRUE) : 
  invalid color name 'c("white", "grey")'
trevorld commented 1 year ago

Currently we "solve this" with the following:

args <- lapply(as.list(params), unlist)

And in basic examples this seems to work fine.

However in next version of {ggplot2} we might perhaps expect certain params to be lists of grid patterns for e.g. fill which me might not want to unlist()? Perhaps instead we want a helper functions that extracts the first element of length-one lists?

Also, would we want to explicitly support pattern_fill being patterns as well?

trevorld commented 12 months ago

Instead of just colors we may want to also allow lists of grid patterns...