Search code examples
rggplot2patchwork

A more elegant way to add a list of tags to resid_compare graph?


My journal requires combined graphs to be labeled by panels. Doing the following gives me the "A" tag only.

library(ggResidpanel); library(patchwork)


penguin_model <- lme4::lmer(heartrate ~ depth + duration + (1|bird), data = penguins)
penguin_model_log2 <- lme4::lmer(log(heartrate) ~ depth + duration + I(duration^2) +
(1|bird), data = penguins)

resid_compare(list(penguin_model, penguin_model_log2)) + labs(tag = list("A" , "B" )) 

switching to labs(tag = c("A" , "B" )) doesn't solve the problem.

enter image description here

Plotting each of the diagnosis graphs and combine them returns what I need. I'm OK doing the patchwork for 2 diagnosis panels but it would be nice to be able to use a one-line command in the resid_compare if I have 6 panels to display.

A <- resid_panel(penguin_model, nrow = 4) + labs(tag = "A")
 
B <- resid_panel(penguin_model_log2, nrow = 4) + labs(tag = "B")

A | B

enter image description here

Note: The images look muddled here but they look OK in knitted pdf.

Any help is much appreciated.


Solution

  • Not a one-liner but one option would be to put your models in a list, use lapply to make the plots for each model, use patchwork::wrap_plots to glue them together and use patchwork::plot_annotation to add the tags:

    library(ggResidpanel)
    library(patchwork)
    library(magrittr)
    
    penguin_model <- lme4::lmer(heartrate ~ depth + duration + (1|bird), data = penguins)
    penguin_model_log2 <- lme4::lmer(log(heartrate) ~ depth + duration + I(duration^2) +
                                       (1|bird), data = penguins)
    
    list(penguin_model, penguin_model_log2) %>% 
      lapply(resid_panel, nrow = 4) %>% 
      wrap_plots() +
      plot_annotation(tag_levels = "A")