grantmcdermott / tinyplot

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

Should `type_function()` be `y` aware? #257

Open grantmcdermott opened 2 weeks ago

grantmcdermott commented 2 weeks ago

It would be cool if we could do something like:

plt(
    weight ~ Time | Diet, data = ChickWeight,
    type = type_function(\(x, y) ave(y, x))
)

Where, in this case, you would get lines of the mean weight per Time period. You'd have to check that the resulting object was the same length as the original object, though.

OTOH maybe this should be a separate type function (type_summary?) that explicitly guarantees the same length by passing appropriate summary functions to ave. Then you could do things like:

plt(
    weight ~ Time | Diet, data = ChickWeight,
    type = type_summary(fun = mean) # or median etc.
)
vincentarelbundock commented 2 weeks ago

That feels like a different type. Maybe something like type_transform(fun), where we would check formals(fun) to see if we should pass it x, y, ymin, ymax, etc. I can imagine, for example, using this to back-transform confidence intervals, so transforming ymin might be useful too.

Would have to think about what constraints to impose on input/output. If we go such a flexible route, I think it should be very strict and raise informative errors. Otherwise users will get frustrated with weird messages.