Search code examples
rggplot2modelsummary

Different colors for different models in faceted modelplot


I cannot seem to be able to color the lines of different models with different colors in the following model:

library(modelsummary)

url <- 'https://vincentarelbundock.github.io/Rdatasets/csv/palmerpenguins/penguins.csv'
dat <- read.csv(url)

# rescale mm -> cm
dat$bill_length_cm <- dat$bill_length_mm / 10
dat$flipper_length_cm <- dat$flipper_length_mm / 10
models <- list(
  "Small model" = lm(bill_length_cm ~ flipper_length_cm, data = dat),
  "Medium model" = lm(bill_length_cm ~ flipper_length_cm + body_mass_g, data = dat),
  "Large model" = lm(bill_length_cm ~ flipper_length_cm + body_mass_g + species, data = dat))

modelplot(models, facet = TRUE)

which results in

enter image description here

I tried with

scale_fill_brewer(palette = "Set1", name = "term")

but doesn't seem to work


Solution

  • You can achieve your desired result by mapping model on the color aes. Afterwards you can set your desired colors using scale_color_brewer or ...

    library(modelsummary)
    library(ggplot2)
    
    modelplot(models, facet = TRUE) +
      aes(color = model) +
      scale_color_brewer(palette = "Set1")
    

    enter image description here