I am using dwplot to plot multiple coefficients across multiple models. p <- dwplot(list(mod1976,mod1980,mod1984))
The plot outputs coefficients colored by model. I want to color the coefficients so that those that are positive and significant are green, negative and significant are red, and all others are grey.
Is this possible?
This was a little harder than I thought; I couldn't figure out how to override dwplot
's colour mapping, but with slightly more effort you can draw your own plot matching the one from dwplot
, with more control.
library(broom)
library(dplyr)
library(dotwhisker)
respvars <- c("mpg", "hp", "wt")
names(respvars) <- respvars ## for .id in map_dfr
predvars <- setdiff(names(mtcars), respvars),
ms <- as.data.frame(scale(mtcars))
mm_list <- purrr::map(respvars,
~ lm(reformulate(predvars, response = .x), data = ms))
mm_pars <- mm_list |>
purrr::map_dfr(tidy, .id = "response", conf.int = TRUE) |>
dplyr::filter(term != "(Intercept)") |>
mutate(cat = ifelse(p.value > 0.05, "ns",
ifelse(estimate > 0, "pos", "neg")))
Unsuccessful attempt to override the colour mapping:
dwplot(mm_list) + aes(shape = model, colour = cat) +
scale_colour_manual(values = c("grey", "blue", "red"))
A plot that works (these aren't exactly the colours you asked for, but that should be easy to change ...)
ggplot(mm_pars, aes(x = estimate, y = term, colour = cat,
## I'm not sure the group mapping is necessary ...
shape = response, group = response)) +
geom_pointrange(position = position_dodge(width = 0.5),
aes(xmin = conf.low, xmax = conf.high)) +
## add a reference line
geom_vline(xintercept = 0, linetype = 2) +
scale_colour_manual(values = c("grey", "blue", "red"), breaks = c("ns", "pos", "neg"))