Reputation: 31
I have a mixed effects linear model and I want to plot it using the sjPlot package. I have added a geom-ribbon layer for the CI of each reg line, but I just can't figure how to change it's fill color. Idealy I'd want each ribbon to be the same color as it's reg line.
Here is my code for the model:
model <- lme(no_stickers_gave_stranger ~ s_negativeAff*parent_model,
data = table4, random = ~1|ifam)
And here is the code for the plot:
plot_model(model = model, line.size = 0.71, type = "int",
colors = sjplot_pal(palette = "metro"),
axis.title = c("Negative Emotionality",
"Number of sticker shared with a stranger"),
title = "Age 6 - LAB-TAB",
legend.title = "Parent Model") +
scale_fill_sjplot(palette = "metro", discrete = T) +
geom_ribbon(aes(ymin = conf.low, ymax = conf.high), colour = NA, alpha = 0.25)
The best I could do is this:
Upvotes: 3
Views: 2675
Reputation: 13793
For whatever reason, setting colors=
within plot_model()
was not working properly for me either. The way around here is to set your color values the same way you're setting the fill values using either scale_color_sjplot()
or scale_color_manual(values=sjplot_pal())
.
Curiously, the two methods both work (to match color and fill aesthetics), but give you different actual colors. I have no answer for why, but this is how to match color and fill. I'm using the example dataset from vignettes on the sjplot site.
library(sjPlot)
library(ggplot2)
data(efc)
fit <- lm(barthtot ~ c12hour + neg_c_7 + c161sex + c172code, data = efc)
cols <- sjplot_pal(palette = "metro")
p <- plot_model(fit, type = "pred", terms = c("c161sex", "c12hour"))
p
Just adding the scale_fill_sjplot()
function changes fill alone:
p + scale_fill_sjplot()
Adding scale_color_sjplot()
fixes that:
p + scale_fill_sjplot() + scale_color_sjplot()
Strangely, if we use the scale_*_manual()
functions and supply the sjplot_pal()
as the reference for values=
, we get different colors, but at least they do match:
p + scale_fill_manual(values=cols) + scale_color_manual(values=cols)
Without knowing more bout the sjPlot
package and plot_model()
, I would guess the difference in color has to do with the package assigning levels and colors a bit differently than ggplot2
does by default (which is either alphabetical or based on the levels()
if you have a factor).
Upvotes: 1