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

How can I save the group of plots produced by check_model to a figure from within a script? #674

Open
ZaidaEMtzMo opened this issue Jan 24, 2024 · 4 comments
Labels
Waiting for response 💌 Need more information from people who submitted the issue

Comments

@ZaidaEMtzMo
Copy link

ZaidaEMtzMo commented Jan 24, 2024

Hello,

Sorry if this is not the right place, but I have been struggling with this all day and have tried to find an answer online but I couldn't.
Your library is amazing and produces the diagnostic plots that I just need. However, I don't seem to find a way to save them.
In the terminal, when I do check_model(analysis_model) it produces the plots without problem, and then I can do ggsave(directory, width=10,height=15,dpi=300) and it gets saved.
However, if I do that from within a script, it saves the latest figure I produced and not the one from check_model, even if I do check_model(analysis_model) right above ggsave...

I have also tried:

diagn_fig=check_model(analysis_model)
ggsave(directory,
       plot = diagn_fig,
       width = 10,
       height = 15,
       dpi = dpi)

But then I get the error:

Error in UseMethod("grid.draw") : 
  no applicable method for 'grid.draw' applied to an object of class "c('check_model', 'see_check_model')"

I am sure there is a way of saving the figures from within a script, but I just can't figure it out, so I would appreciate any help.
Thank you!

@ZaidaEMtzMo
Copy link
Author

After multiple hours, I finally checked the "see" library and figured out how to get it:

# Plot the result of check_model
diagn_fig = plot(check_model(analysis_model))
ggsave(directory,
       plot = diagn_fig,
       width = 10,
       height = 15,
       dpi = dpi)

And that's it... (sorry for the post, and great work in this tool)

@ZaidaEMtzMo
Copy link
Author

After doing an update (easystats::easystats_update()), it no longer works and I am back to the start.

@bwiernik
Copy link
Contributor

When you say it doesn't work, what exactly is happening?

Can you post your script exactly so that we can look at the code?

Note that when you knit an RMarkdown/Quarto script, the image files are all saved in a folder in the file directory and you can access them from there.

@strengejacke strengejacke added the Waiting for response 💌 Need more information from people who submitted the issue label Mar 18, 2024
@jarbet
Copy link

jarbet commented Jun 12, 2024

I also can't figure out how to save plots to file (since I can't get them to display in my Rstudio window either).

Here is some example code that tries 3 different approaches to plotting, all of which produce errors:

library(lme4);
#> Loading required package: Matrix
library(performance);

fit.lmer <- lmer(Reaction ~ Days + (Days | Subject), data = sleepstudy);

### error when trying to plot in Rstudio window
check_model(fit.lmer);
#> Error: The RStudio 'Plots' window is too small to show this set of plots. You
#>   may try one of the following steps to resolve this problem.
#>   
#> - To fix this issue, please make the window larger.
#>   
#> - If this doesn't help, try to reset your zoom settings. In RStudio, go
#>   to Menu "View > Actual Size" and then retry.
#>   
#> - If this still doesn't resolve your problems, you may check whether
#>   your apps are rescaled. On Windows, this can be done in the display
#>   settings (Start > Settings > System > Display, "Scale and layout").
#>   Reduce the scaling and try again.
#>   
#> - Finally, you can try to decrease the base font-size of your theme
#>   before plotting. Load `library(ggplot2)` and run:
#>   `theme_set(theme_classic(base_size = 6))`

### error when trying to save to png file:
png('temp.png')
check_model(fit.lmer);
#> Error: The RStudio 'Plots' window is too small to show this set of plots. You
#>   may try one of the following steps to resolve this problem.
#>   
#> - To fix this issue, please make the window larger.
#>   
#> - If this doesn't help, try to reset your zoom settings. In RStudio, go
#>   to Menu "View > Actual Size" and then retry.
#>   
#> - If this still doesn't resolve your problems, you may check whether
#>   your apps are rescaled. On Windows, this can be done in the display
#>   settings (Start > Settings > System > Display, "Scale and layout").
#>   Reduce the scaling and try again.
#>   
#> - Finally, you can try to decrease the base font-size of your theme
#>   before plotting. Load `library(ggplot2)` and run:
#>   `theme_set(theme_classic(base_size = 6))`
dev.off();
#> quartz_off_screen 
#>                 2

### error when using ZaidaEMtzMo's approach
diagn_fig = plot(check_model(fit.lmer))
#> Error: The RStudio 'Plots' window is too small to show this set of plots. You
#>   may try one of the following steps to resolve this problem.
#>   
#> - To fix this issue, please make the window larger.
#>   
#> - If this doesn't help, try to reset your zoom settings. In RStudio, go
#>   to Menu "View > Actual Size" and then retry.
#>   
#> - If this still doesn't resolve your problems, you may check whether
#>   your apps are rescaled. On Windows, this can be done in the display
#>   settings (Start > Settings > System > Display, "Scale and layout").
#>   Reduce the scaling and try again.
#>   
#> - Finally, you can try to decrease the base font-size of your theme
#>   before plotting. Load `library(ggplot2)` and run:
#>   `theme_set(theme_classic(base_size = 6))`
ggplot2::ggsave('temp.png',
       plot = diagn_fig,
       width = 10,
       height = 15,
       dpi = 300)
#> Error in eval(expr, envir, enclos): object 'diagn_fig' not found

Created on 2024-06-11 with reprex v2.0.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Waiting for response 💌 Need more information from people who submitted the issue
Projects
None yet
Development

No branches or pull requests

4 participants