rggplot2visualizationlme4

Ggplot visualization of Lmer output


I'm trying to visualize lmer output with ggplot, but cannot get the result I want.

The code looks as follows:

model <- lmer(Fluency ~ Roundnr * relevel(as.factor(Condition), ref='C3') * block + (1|Participant), data=data_aggregate, REML=FALSE)

ggplot(data_aggregate,aes(Roundnr, Fluency, group=interaction(Participant, Condition), col=Condition)) + 
  facet_grid(~block, scales="free", space="free_x") +
  geom_line(aes(y=predict(model), lty=Condition), linewidth=0.8) +
  geom_point(alpha = 0.3) + 
  geom_hline(yintercept=0, linetype="dashed")

The result looks like this:

enter image description here

(code is adapted from the answer to this question: plot mixed effects model in ggplot)

However, I would like to show 1 line per condition in stead of a line for every participant. I cannot really share the full data but it is data of roughly 60 participants who each did a task 8 times (4 times in A, 4 times in B). Changing the it such that group=Condition doesn't work, it gives the following weird result: enter image description here

Any suggestions for how this can be achieved?


Solution

  • The simplest solution, I think is to change:

    ggplot(data_aggregate,aes(Roundnr, Fluency, group=interaction(Participant, Condition), col=Condition)) + 
      facet_grid(~block, scales="free", space="free_x") +
      geom_line(aes(y=predict(model), lty=Condition), linewidth=0.8) +
      geom_point(alpha = 0.3) + 
      geom_hline(yintercept=0, linetype="dashed")
    

    into:

    ggplot(data_aggregate,aes(Roundnr, Fluency, group=interaction(Participant, Condition), col=Condition)) + 
      facet_grid(~block, scales="free", space="free_x") +
      geom_line(aes(y=predict(model, re.form = NA), lty=Condition), linewidth=0.8) +
      geom_point(alpha = 0.3) + 
      geom_hline(yintercept=0, linetype="dashed")
    

    This way your random effects will be disregarded for the lines, creating only one line for a "prototype" of each combination of factors.