Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

sjplot plot_model with own color palette

I apologize if this is a very basic question, but I’m having trouble defining colors when using plot_model in sjplot. As an example, I have run a simple multilevel regression model where 76 participants represent clusters. Each participant responded to a bunch of questions 16 times over the course of 4 days. The model goes

m<-lmer(happy ~ soc_smc + (soc_smc | id), data=data)

I want to plot the random slopes with plot_model:

p<-plot_model(m, type="pred", terms=c("soc_smc", "id"), pred.type="re", ci.lvl = NA)

Which works nicely, however sjplot base color palettes apparently have max 9 colors and I get 9 colored slopes and 67 grey ones.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I tried to create and use my own palette, e.g.

c1<-colors(distinct=T)
mycols<-sample(c1, 76)
p<-plot_model(m, type="pred", terms=c("soc_smc", "id"), pred.type="re", ci.lvl = NA, colors = "mycols")

However, I get the error "Error: Unknown colour name: mycols"
Same with colorspace

library(colorspace)
mycols<-qualitative_hcl(76)
p<-plot_model(m, type="pred", terms=c("soc_smc", "id"), pred.type="re", ci.lvl = NA, colors = "mycols")

Again plot_model says Error: "Unknown colour name: mycols"

I’m very inexperienced in creating color palettes so maybe I’m doing something wrong there, but why plot_model doesn’t seem to recognize the palette object? Or am I reading the error wrong?

I don’t actually need 76 distinctive colors (and that would be impossible too), however I’d like to get some color for every slope, and most importantly to know what I’m doing wrong.

>Solution :

You have to pass you color palette to the colors argument without quotes.

Using one of the default examples from sjPlot::plot_model:

library(sjPlot)
library(lme4)
#> Loading required package: Matrix
library(colorspace)

mycols <- qualitative_hcl(76)

m <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy)

plot_model(m,
  type = "pred", terms = c("Days", "Subject"),
  pred.type = "re", ci.lvl = NA, colors = mycols
)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading