2

I want to plot the regression between a response variable and one of the explanatory variables in a mixed effects model while also showing the data points. For this I am using the function plot_model(). I would also like the data points to have different colours or shapes according to the random effect. I do not want to plot the different regression lines for each level of the random effect. Is this possible with plot_model?

This is the data:

dat <- data.frame(c(1, 1,  1,  1,  1,  1,  1,  4,  1,  2,  1,  2,  3,  1,  
                             5,  1,  2,  2,  1,  1,  2,  2,  4,  3,  5,  5,  4,  4,
                             8,  4, 13,  8,  6,  5,  6,  5, 4,  8,  6,  2,  3,  9, 7,
                             4,  8,  8, 10, 10,  3,  3), 
                           c(427.0, 110.0, 99.0, 450.0, 182.0,  34.0,  43.0, 105.0, 
                             22.0, 108.0,  85.0, 142.0, 81.0, 109.0,  89.0, 183.0, 
                             106.0, 19.3, 13.5,  36.0, 55.0, 36.0, 128.0,  38.2, 103.0, 
                             105.0,  91.0,  94.0, 106.0,  96.0, 108.0, 103.0, 122.0, 
                             103.0, 112.0, 190.0, 112.0, 77.0,  60.0, 148.0, 161.0, 
                             44.0, 188.0,  44.0,  88.0,  90.0, 124.0, 120.0, 212.0, 309.0),
                           c(32.3, 27.0, 26.7, 33.0, 28.4, 26.7, 28.0, 30.0, 26.5, 
                             28.4, 29.0, 30.4, 25.8, 26.4, 24.2, 28.6, 29.4, 27.7, 
                             25.5, 27.3, 27.4, 29.2, 21.5, 25.4, 27.8, 27.8, 30.2, 
                             30.2, 30.9, 30.0, 30.3, 31.5, 31.3, 31.1, 32.5, 32.0, 
                             32.2, 32.9, 32.9, 31.3, 32.3, 29.4, 31.5, 29.8, 26.7,
                             26.7, 29.9, 29.9, 32.1, 32.1),
                           c(7.73, 1.83, 2.97, 7.66, 1.80, 1.17, 0.66, 6.04, 1.72, 
                             1.25, 2.27, 1.65, 0.16, 0.16, 0.26, 0.11, 0.17, 0.15, 
                             0.20, 0.21, 0.15, 0.10, 1.83, 1.84, 7.67, 7.67, 5.81, 
                             5.81, 3.95, 3.45, 1.15, 3.07, 3.61, 3.35, 0.86, 2.20, 
                             4.31, 1.64, 1.14, 3.55, 4.81, 1.36, 1.94, 3.36, 3.30,
                             3.30, 2.66, 2.66, 6.55, 6.55),
                           c(94, 151, 111,  94,  87, 128, 126,  39, 130, 107,  93, 
                             110,  76, 309,  38,  19,  13, 113, 110, 108, 103,  30,
                             58, 151,  55,  55,  42, 42,  29,  37,  20,  20,  61, 
                             21,   3,   8,  28,  33,  32,  12,  38,  -7,  -5,  74,  
                             22,  22,  10,  10,  22, 22),
                           c("2005", "2005", "2005", "2005", "2005", "2005", "2005",
                             "2018", "2005", "2005", "2005", "2005", "2010", "2010",
                             "2010", "2010", "2010", "2010", "2010", "2010", "2010",
                             "2010", "2012", "2012", "2017", "2017", "2017", "2017",
                             "2017", "2017", "2017", "2017", "2017", "2017", "2018",
                             "2018", "2018", "2018", "2018", "2018", "2018", "2018",
                             "2018", "2018", "2018", "2018", "2018", "2018", "2018", 
                             "2018"))
colnames(dat) <- c("SpR", "Dep", "Sal", "Chla", "Ice", "Yr")

This is the mixed effects model:

MEM <- lmer(formula = SpR ~ (1|Yr) +  
                Dep + Sal + Chla + Ice + 
                Dep*Ice, 
              data = dat)

This is the code I'm using to plot the regression between SpR and Dep:

MEMpld <- plot_model(MEM, 
                   type = "pred", 
                   pred.type = "fe", 
                   terms = c("Dep"), 
                   show.data = TRUE,
                   col = "black",
                   title = "",
                   jitter = 0.0002, 
                   dot.size = 3.5,
                   dot.alpha=1)

This is the figure that I get from running this code. I would like the dots to have different colours according to the year:

plot of regression line + points

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
Sarah
  • 23
  • 3
  • 1
    If you wnat tested code you should produce a dataset for demonstration. – IRTFM Aug 28 '21 at 14:47
  • Sometimes, much more interesting visuals can be used with `ggplot2`. Unfortunately, without data, we cannot help you. – Marek Fiołka Aug 28 '21 at 18:20
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Aug 28 '21 at 21:03
  • Thank you for your responses. I have added the data and a figure. I have used ggplot2 to make a lot of edits to the figure but I can't change the colour of the dots to match the Year in which they were sampled. – Sarah Aug 29 '21 at 09:31
  • Hello, I'd recommend using these two pages to help you make the graph that you want. First, you have to make the plot with ggPredict instead of sjPlot, as it will let you manipulate the plot easily: https://cran.r-project.org/web/packages/ggiraphExtra/vignettes/ggPredict.html Then you'll want to use the examples on this page to scale your year by colour. This will likely be done with code that is similar to `mapping=aes(color=Yr)` . You'll find examples of with other data here: http://www.sthda.com/english/wiki/ggplot2-scatterplot-easy-scatter-plot-using-ggplot2-and-r-statistical-software – sjp Aug 30 '21 at 10:58
  • Thank you @sjp, these links are helpful but they don't seem to work with the mixed effects model because the model is an S4 class object. – Sarah Aug 31 '21 at 17:37

1 Answers1

2

If you set show.data = FALSE in your plot_model() call and then add the points separately it seems to work OK:

MEMpld + 
   geom_point(data=dat, aes(x=Dep, y=SpR, colour = Yr))  +      
   scale_colour_discrete(name = "Yr") + 
   guides(fill="none")

plot with points

I'm not sure why the legend looks a little bit funky; I tried to turn off the fill legend (which I think is set internally) but it doesn't appear to do what I wanted.

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • Thank you so much, this works! I tried changing the legend using theme(legend.key = element_rect(fill = "white", colour = "white")) but that doesn't work. But this is not an issue, I'm happy with the final plot, thank you! – Sarah Sep 02 '21 at 08:31