Repository navigation
Barebones support for <GridPattern> fills. - #5299
Conversation
|
Alright, time for a small update.
Remaining issues:
Some examples for all pattern options: Linear gradients work as expected. devtools::load_all("~/packages/ggplot2")
#> ℹ Loading ggplot2
#> Warning: package 'testthat' was built under R version 4.3.1
p <- ggplot(mpg, aes(drv))
grad <- linearGradient(rainbow(10))
p + geom_bar(fill = grad, alpha = 0.5)Radial gradients work as expected. grad <- radialGradient(rainbow(10))
p + geom_bar(fill = grad, alpha = 0.5)Tiled patterns work as expected. tile <- pattern(
circleGrob(r = unit(1, "mm"), gp = gpar(fill = "black")),
extend = "repeat", width = unit(3, "mm"), height = unit(3, "mm")
)
p + geom_bar(fill = tile, alpha = 0.5)The {gridpattern} patterns work if wrapped in pattern <- pattern(gridpattern::grid.pattern_circle(
density = 0.5, grid = "hex_circle", fill = c("blue", "yellow", "red"),
draw = FALSE
))
p + geom_bar(fill = pattern, alpha = 0.5)Example of error message when device doesn't support feature: png(type = "windows")
p + geom_bar(fill = pattern, alpha = 0.5)
#> Error in `geom_bar()`:
#> ! Problem while converting geom to grob.
#> ℹ Error occurred in the 1st layer.
#> Caused by error in `fill_alpha()` at ggplot2/R/geom-rect.R:54:6:
#> ! Unable to check the capabilities of the png device.
dev.off()Created on 2023-10-06 with reprex v2.0.2 |
Awesome update @teunbrand ! It's possible to edit the angle of linear gradients? |
Yes, but that all happens in |
|
Looks good! I observe that > fill_alpha(list(c("blue", "red")), 0.5)
Error in `fill_alpha()`:
! fill must be a valid colour or list of <GridPattern> objects.
Run `rlang::last_trace()` to see where the error occurred.Future extension authors perhaps may want it to support color vectors greater than one? A future release of |
|
Ah right, perhaps the error message should be clearer, but you can give it a bare vector of colours, they don't need to be in a list. fill_alpha(c("blue", "red"), 0.5)
> [1] "#0000FF80" "#FF000080" |
I observe that if you do something like I understand that in the base Thought that maybe this would be an additional special case it may be nice for |
I can see the utility of that but I'm having a hard time imagining why you'd need |
|
|
Alright I have given it some more consideration, but the most important thing to me is that grid::gpar(fill = c("red", "blue"))
#> $fill
#> [1] "red" "blue"
grid::gpar(fill = list("red", "blue"))
#> Error in validGP(list(...)): 'fill' gpar list components must all be patterns
grid::gpar(fill = list(list("red", "green"), "blue"))
#> Error in validGP(list(...)): 'fill' gpar list components must all be patternsCreated on 2023-10-12 with reprex v2.0.2 This PR is allowing mixed inputs of * I'm using the term 'ragged list' to mean a list where Now, aside from the problem that ragged lists of colours aren't valid input for Lastly, and this is more of a minor point, if Thus, my advice for ragged lists is to handle these in your package according to your needs. ggplot2 is MIT licenced so you're free to adapt |
|
This should probably live in another PR, but |
|
Thanks for the review Thomas! |




This PR sprang forth from #3997.
Briefly, it attempts to provide some barebones support for {grid}'s new pattern features.
Less briefly:
Essentially, the only piece of ggplot2's internals that hampered using grid's patterns was the
alpha()function that has restrictions on what it considers a colour. To lift this restriction, there is now afill_alpha()function exported (for extension devs), that can set alpha on any of {grid}'s 3 pattern classes.What can you do with this PR:
geom_rect(fill = <(list of) GridPattern>)outside theaes().geom_rect(aes(fill = something)) + scale_fill_manual(values = <list of GridPattern>).What this PR doesn't do:
I'm also pinging @trevorld and @coolbutuseless because they might have some opinions about this PR and experience with patterns.
A small demo:
Created on 2023-05-06 with reprex v2.0.2