Search code examples
rcolorssjplotmultilevel-analysis

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.

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
    )