trevorld / ggpattern

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

[FEAT] Allow alpha gradients in addition to the colour gradients? #101

Closed josswright closed 11 months ago

josswright commented 11 months ago

I came across this package trying to create (effectively) a geom_line with a vertical gradient that fades from the line downwards. (An old StackOverflow comment here gives the idea: https://stackoverflow.com/questions/55764368/create-a-geom-line-or-similar-with-fading-alpha-below )

I can do something similar by using a geom_area_pattern, but the only available fade is from colour to colour, which covers gridlines and is a bit fragile between theme backgrounds. Could there be an equivalent pattern_alpha2 equivalent to pattern_fill2?

(It would also be amazing if this could tie into an equivalent to #100 so that you could control the 'tightness' of the alpha fade to the line.)

trevorld commented 11 months ago

Have you tried something like fill='#00000000', pattern_fill='#FF0000FF', and pattern_fill2='#FF000000' yet? {ggpattern} should de facto already support alpha gradients if you add the alpha channels to the fills. scales::alpha() can help figure these out...

josswright commented 11 months ago

Oh, it never occurred to me that the pattern fills might support #RGBA! I'll have a play now and see. Thanks!

EDIT: Works! For the effect I'm trying to get I think I'd still need a way to fade to zero more quickly than just fading linearly to the origin, but this is already fairly amazing. :) I've posted some images, and this as a solution, to the StackOverflow question I linked in the original issue.

8kGD0

trevorld commented 11 months ago

For the effect I'm trying to get I think I'd still need a way to fade to zero more quickly than just fading linearly to the origin

{gridpattern} currently supports either linear or radial gradients. (Depending on graphics device support) it uses either R's built in gradient pattern support or a rasterized fallback using magick. Both of these software options only support linear or radial gradients.

I think however you could build a custom "raster" pattern in {gridpattern} that takes an original color and then uses scales::alpha() with different alpha values to compute "#RGBA" values for different "pixels" to achieve a faster fade: https://trevorldavis.com/R/gridpattern/dev/articles/developing-patterns.html