grantmcdermott / tinyplot

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

Should we support `symbols(2)`? #29

Closed grantmcdermott closed 1 year ago

grantmcdermott commented 1 year ago

Mostly for bubble charts. You can imagine the background colour below being assigned by group (as opposed to random recycling here).

with(
    data.frame(state.x77),
    {
        symbols(
            x = Income, y = Life.Exp,
            circ = sqrt(Population / pi),
            inches = .25,
            fg = "white",
            bg = palette.colors(n = 4, alpha = 0.5)[-1],
            main = "Life expectancy among US states (1977)"
        )
    }
)

Created on 2023-04-17 with reprex v2.0.2

grantmcdermott commented 1 year ago

Upon reflection, it's probably better (easier) from a maintenance perspective just to do this via cex. Here, using a similar rescaling factor to ggplot2.

rscl = function (x) {
    from = range(x)
    to = c(1, 6)
    (x - from[1])/diff(from) * diff(to) + to[1]
}

with(
    data.frame(state.x77),
    plot(
        x = Income, y = Life.Exp,
        pch = 21,
        cex = rscl(Population),
        col = "white",
        bg = palette.colors(n = 4, alpha = 0.5)[-1],
        main = "Life expectancy among US states (1977)"
    )
)

Created on 2023-07-27 with reprex v2.0.2

grantmcdermott commented 1 year ago

Closing in favour of #48.