grantmcdermott / tinyplot

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

`plt_add()` #246

Closed vincentarelbundock closed 2 weeks ago

vincentarelbundock commented 2 weeks ago

I’ve been trying to think about ways to make it easier to layer plots. We have add=TRUE, but it is not very convenient, because we need to repeat all the same arguments in every call.

I can’t decide if this is a good or a terrible idea, but here’s a proof of concept for you to comment on:

  1. Every time we do plt(), we save the full call somewhere (global option or other).
  2. New function plt_add()
    • Takes the last call,
    • Modifies the explicitly-given argument
    • Sets add=TRUE
    • Evaluates the modifed call

This proof of concept PR allows things like:

library(tinyplot)

plt(Sepal.Width ~ Sepal.Length | Species,
    data = iris, 
    type = type_lm())

plt_add(type = "p")

Note: Facets don’t seem to work in this PR.

grantmcdermott commented 2 weeks ago

This was exactly what I was thinking of in the digression here: https://github.com/grantmcdermott/tinyplot/issues/236#issuecomment-2444939778

Great minds etc. ;-)

I really like the idea and think that this is a feature that would get a lot of use. I'll be able to take a look at it tomorrow (or whenever you move it out of draft).

vincentarelbundock commented 2 weeks ago

@grantmcdermott I added docs and a test. You might want to look at this now.

grantmcdermott commented 2 weeks ago

This is great. Thanks @vincentarelbundock!