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

Is there a way to directly access or export the plot returned by plotResiduals()? #395

Closed
smsfrn opened this issue Nov 27, 2023 · 3 comments

Comments

@smsfrn
Copy link

smsfrn commented Nov 27, 2023

I'm likely missing something obvious, but the plot itself doesn't appear to be a component in the returned object, so I'm having trouble accessing it to save programmatically. Any ideas?

@smsfrn
Copy link
Author

smsfrn commented Nov 27, 2023

This works for saving one-off plots, but I'm still hoping to access the plot directly from the plotResiduals() object to support more complex workflows (i.e. looping through multiple predictors, saving the residual plot for each in a list, and generating a gridded figure with all the plots).

library(tidyverse)
library(lme4)
library(DHARMa)
library(here)

dat <- iris %>%
  mutate(Petal.Length_factor = ifelse(Petal.Length < 4.2, "A", "B"))

mod <- lme4::lmer(Sepal.Width ~ Species + Petal.Width + (1 | Petal.Length_factor),
                  data = dat)

simulationOutput <- simulateResiduals(fittedModel = mod)

var <- "Petal.Width"

output_path <- here(paste0("residual_plot_", var, ".png"))

png(output_path)
plotResiduals(simulationOutput, dat %>% pull(var))
dev.off()

@florianhartig
Copy link
Owner

Hello Sam,

can you give me an example of another plotting function where this works?

Note that DHARMa is using base R and and not ggplot for plotting

Best,
F

@smsfrn
Copy link
Author

smsfrn commented Nov 27, 2023

Oops, nope. I misremembered what could be done with base graphics. I'll close this!

@smsfrn smsfrn closed this as completed Nov 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants