I am having trouble sending a performance::check_model()
to a pdf (or the plot window) without it printing twice when I add a title with patchwork::plot_annotation()
. check_model
is supposed to use patchwork, however this is printing once without the title, then printing again with it. Here is an example:
library(lme4)
library(tidyverse)
library(patchwork)
library(performance)
library(see) # edit added, it might be necessary, see comments
tCars <- lmer(mpg ~ qsec + (1 | cyl) + hp, mtcars)
tCars
print(plot(check_model(tCars,
panel = TRUE,
size_dot = 1,
size_line = 0.5,
base_size = 8,
size_title = 10,
size_axis = 6)) & # I use & or + here and both same.
plot_annotation(title = "best Cars"))
# I also tried separating the print() and plot_annotation(), but same result:
print(plot(check_model(tCars,
panel = TRUE,
size_dot = 1,
size_line = 0.5,
base_size = 8,
size_title = 10,
size_axis = 6))) + # I use & or + here and both same.
plot_annotation(title = "best Cars")
Am I doing something wrong? I would like it to print once. This is very perplexing.
EDIT =============
Regarding comments below, I used print()
without thinking as this is sometimes necessary to get some plots to go to the pdf, however when you remove print() it does the same thing.
plot(check_model(tCars,
panel = TRUE,
size_dot = 1,
size_line = 0.5,
base_size = 8,
size_title = 10,
size_axis = 6)) & # I use & or + here and neither work.
plot_annotation(title = "best Cars")
plot(check_model(tCars,
panel = TRUE,
size_dot = 1,
size_line = 0.5,
base_size = 8,
size_title = 10,
size_axis = 6)) & # I use & or + here and neither work.
plot_annotation(title = "best Cars")
This avoids creating a second plot:
print(plot(check_model(tCars,
panel = TRUE,
size_dot = 1,
size_line = 0.5,
base_size = 8,
size_title = 10,
size_axis = 6)) +
plot_annotation(title = "best Cars"),
newpage = FALSE)
I believe this still plots twice (and there is really no way of avoiding that without modifying see:::plot.see_check_model
). I think it just overplots the first plot.
I'm not sure it is documented anywhere. I studied print
and plot
methods that aren't exported until I ultimately arrived at the code of patchwork:::print.patchwork
and saw the newpage
parameter.
For a PDF (where it matters), I would just do this:
p <- plot(check_model(tCars,
panel = TRUE,
size_dot = 1,
size_line = 0.5,
base_size = 8,
size_title = 10,
size_axis = 6))
pdf("plot.pdf")
print(p + plot_annotation(title = "best Cars"))
dev.off()