Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

print vs plot methods #119

Open
bwiernik opened this issue May 11, 2021 · 1 comment
Open

print vs plot methods #119

bwiernik opened this issue May 11, 2021 · 1 comment
Labels
Question ❓ Further information is requested

Comments

@bwiernik
Copy link
Contributor

bwiernik commented May 11, 2021

The R convention is that print() should return the original object invisibly. In base R, plotting is just a side effect, and the plot object cannot be saved. ggplot changes the latter behavior and has functions that produce objects of class "ggplot", which has corresponding plot and print methods to actually render the plot. These methods follow the convention that print() should return the original object invisibly (the object here being the plot).

see currently sits somewhat awkwardly between these two conventions. When the print() method is called for objects of class see_*, the various plots are generated and rendered, then the original object is returned (not the plot). What makes this feel awkward is that, although the plots are ggplots, they are never returned as objects, so users cannot extend them further.

The awkwardness goes a little further in that we don't currently make much distinction between, eg., model check results from check_model() and the plots of those results. So a user might expect to be able to do something like this:

library(performance)
library(ggplot2)
library(see)

m <- lm(mpg ~ hp, data = mtcars)
cc <- check_model(m)
cc + theme_bw()

I don't think we can reasonably change the whole underlying class dispatch for easyverse functions, but I think we really should make it possible for a user to get to a place where standard ggplot conventions apply.

Here's my idea. I suggest we extend the API to have parallel print() and plot() methods for each class. print() will continue the current behavior--generate whatever output and plots, return the original data. plot() however will generate the plots and return them as "ggplot" or "patchwork" objects. This would enable:

library(performance)
library(ggplot2)
library(see)

m <- lm(mpg ~ hp, data = mtcars)
cc <- check_model(m)
plot(cc) + theme_bw()

Edit: fixed example code based on Daniel's comments.

@bwiernik bwiernik added the Question ❓ Further information is requested label May 11, 2021
@cedricbatailler
Copy link

I'm integrating performance in a R package which uses several OLS regressions to test statistical mediation (JSmediation) in order to provide helpers to check assumptions. I'd love to provide helpers to show the diagnostic plots of the several underlying models.

Right now, I cannot edit the plot produced by see to add, for example, a title, but I'd love too!

I have the feeling that the feature offered here would help a lot such integration of see in other packages.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question ❓ Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants