grantmcdermott / tinyplot

Lightweight extension of the base R graphics system
https://grantmcdermott.com/tinyplot
Apache License 2.0
226 stars 7 forks source link

Support alpha = <numeric> and/or bg = <numeric> as shortcut for adjustcolor(col, <numeric>)? #129

Closed zeileis closed 3 months ago

zeileis commented 7 months ago

Should we support setting bg = <numeric> in [0, 1] as a shortcut for bg = adjustcolor(col, <numeric>) (as discussed in https://github.com/grantmcdermott/tinyplot/pull/122)?

This would be handy when we want to use pch = 21:25 with some border col and then automatically use a semi-transparent version of the same color as the bg fill. But maybe this is too specialized and esoteric to implement a special shortcut.

Application example:

p <- hcl.colors(120, rev = TRUE)[11:110]
tinyplot(lat ~ long | depth, data = quakes, pch = 21, cex = 3, lwd = 2, col = p, bg = adjustcolor(p, 0.1))

The idea would be to abbreviate the last argument to just bg = 0.1.

tinyplot with bg being a semi-transparent version of the main col

grantmcdermott commented 5 months ago

Related to this, I wonder if we should support an alpha = <[0,1]> argument for easy transparency of colours?

On a recent project, I find myself doing variants of plt(many_points, col = adjustcolor("black", 0.1). It would be nicer to just write plt(many_points, alpha = 0.1). I'm not sure if this could be easily integrated with your fill = [0,1] idea, or whether they should exist as separate arguments. Probably the latter.

zeileis commented 5 months ago

I think that this will be nifty for some - and not confusing for those who don't need it. So it sounds like a useful addition to me.

... and I agree that it should be a separate argument.

grantmcdermott commented 3 months ago

FWIW, this now works in the cran branch.

library(tinyplot)

tinyplot(lat ~ long | depth, data = quakes, cex = 3, pch = 21, bg = 0.2)

Similarly, for the alpha argument (which affects both the main col and fill, if applicable):

tinyplot(lat ~ long | depth, data = quakes, cex = 3, pch = 19, alpha = 0.2)

Created on 2024-06-17 with reprex v2.1.0