grantmcdermott / tinyplot

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

Support pointrange character / factor x-axis #38

Closed grantmcdermott closed 1 year ago

grantmcdermott commented 1 year ago

One special case that we might want to think about separate support for is point-range plots (following @vincentarelbundock's PR in #35). In particular, we probably want to handle the x-axis carefully if we are passing a vector of characters or factors, e.g. coefficient names.

At present, we have to manually convert the x axis to a numeric first...

library(plot2)
par(pch = 19)

mod = lm(mpg ~ hp + factor(cyl), mtcars)
coefs = data.frame(names(coef(mod)), coef(mod), confint(mod))
coefs = setNames(coefs, c("x", "y", "ymin", "ymax"))

with(
    coefs,
    plot2(
        x = 1:4, # <<-- Problem: has to be numeric ATM
        y = y,
        ymin = ymin,
        ymax = ymax,
        type = "pointrange"
    )
)

... whereas, we'd ideally just be able to pass it the x variable directly and it would handle labels appropriately.

# aspirational code example that doesn't currently work
with(
    coefs,
    plot2(
        x = x,
        y = y,
        ymin = ymin,
        ymax = ymax,
        type = "pointrange"
    )
)

Created on 2023-06-19 with reprex v2.0.2

Should be easy to do. But just flagging so that we don't inadvertently impose/override with unexpected behaviour upstream.

Originally posted by @grantmcdermott in https://github.com/grantmcdermott/plot2/issues/2#issuecomment-1597829275