0

I'd like to plot multiple models side-by-side using sjPlot::plot_models. Yet, when I run the following code:

set.seed(10)
a <- sample(c("low", "medium", "high"), 100, replace = T)
b <- rnorm(100)
c <- rnorm(100)

mod1 <- lm(b ~ a)
mod2 <- lm(c ~ a)

sjPlot::plot_models(mod1, mod2, grid = T)

Then the left plot will be red and the right plot will be blue. Is there a way to get the positive coefficients to be blue and the negatives (lower right) to be red?

Quinten
  • 35,235
  • 5
  • 20
  • 53
Tea Tree
  • 882
  • 11
  • 26

1 Answers1

1

You could use ggplot_build to conditionally change the colors in the layers by negative y values like this:

library(ggplot2)
library(dplyr)
p <- sjPlot::plot_models(mod1, mod2, grid = T)

q <- ggplot_build(p)
q$data[[2]] = q$data[[2]] %>%
  mutate(colour = case_when(y < 0 ~ '#E41A1C', 
                            TRUE ~ '#377EB8'))
q$data[[3]] = q$data[[3]] %>%
  mutate(colour = case_when(y < 0 ~ '#E41A1C', 
                            TRUE ~ '#377EB8'))

q <- ggplot_gtable(q)
plot(q)

Created on 2023-03-11 with reprex v2.0.2

Quinten
  • 35,235
  • 5
  • 20
  • 53