0

Say, I have a linear mixed-effects model:

## create data    
iris$group = c(rep('A', each = 75),
               rep('B', each = 75))

iris$id = rep(c(1:10), each = 15)

## assign contrasts
iris$group <- factor(iris$group, levels=c('A', 'B'))
(contrasts(iris$group) <- matrix(dimnames=list(levels(iris$group), c('.sum.group')), c(-0.5, 0.5), nrow=2))

## build the model
  
LMM = lmer(Sepal.Length ~ 1 +
             
             Sepal.Width +
             Petal.Length +
             Petal.Width + 
             
             group*Petal.Width +

             (1|id),
           
           data = iris,
           control=lmerControl(optimizer="bobyqa",
                               optCtrl=list(maxfun=2e5)),
           REML = FALSE)

Now, I need to plot the interaction group*Petal.Width from the model using the function ggemmeans. Exactly this function must be used in my case. The documentation for the function says, it gives a standard ggplot-object as an output. However, I couldn't figure out how to adjust its aesthetics, and it seems that it's not exactly how ggplot would behave. Here is the best that I could achieve:

ggemmeans(LMM, terms = c("Petal.Width", "group")) %>% plot()+ 
  geom_line(aes(linetype=group))+ 
  aes(linetype = group)+
  theme(legend.title = element_text(size=30),
        legend.position = 'top',
        legend.key.size = unit('1.5', 'cm'),
        axis.title.y = element_text(size = rel(2), angle = 90),
        axis.title.x = element_text(size = rel(2)),
        axis.text.x = element_text(size=20),
        axis.text.y = element_text(size=20))+ 
  scale_colour_manual(values = c("orangered", "purple"))

enter image description here

Now, how could I at least

  1. Remove the thin dashed line behind the thick dashed one?
  2. Change the color of the confidence intervals to the color of the lines? (now the plot is using standard colors for CIs, whatever color I assign to the lines)
  3. Make sure these CIs reflect 95% CIs and not something else? (e.g., in a similar function plot_model, there's an argument for controlling this)
Alex M
  • 129
  • 1
  • 7

1 Answers1

1

The fill and color are mapped to a variable called group_col created by ggemmeans, so you can do:

ggemmeans(LMM, terms = c("Petal.Width", "group")) %>% 
  plot() + 
  aes(linetype = group_col) +
  theme(legend.title = element_text(size=30),
        legend.position = 'top',
        legend.key.size = unit('1.5', 'cm'),
        axis.title.y = element_text(size = rel(2), angle = 90),
        axis.title.x = element_text(size = rel(2)),
        axis.text.x = element_text(size=20),
        axis.text.y = element_text(size=20))+ 
  scale_colour_manual("group", values = c("orangered", "purple")) +
  scale_fill_manual("group", values = c("orangered", "purple"),
                    guide = "legend") +
  scale_linetype_manual("group", values = 1:2) +
  guides(fill = guide_legend(override.aes = 
                               list(fill = c("orangered", "purple"))))

enter image description here

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • Thank you, this solves the problem with the color of the confidence intervals. But (1) how can I now change the thickness of the lines? And (2) ensure that the shaded areas represent 95% CIs? – Alex M Feb 13 '23 at 08:08