rplotgraphinteraction

Is there a way to customise panel titles in an effects plot?


I have created the plot below to visualize interactions but I was wondering if I can customize the parts of the plot that say education = College, education = Graduate, education = High School.

Code:

# Generate example data
set.seed(123)
education <- factor(rep(c("High School", "College", "Graduate"), each = 30))
study_hours <- rep(c(10, 20, 30), each = 10, times = 3)
exam_scores <- rnorm(90, mean = 70, sd = 10)

# Create dataframe
example_df <- data.frame(education, study_hours, exam_scores)
# Fit the model with interaction term
library(glmmTMB)
models_example <- glmmTMB(exam_scores ~ education * study_hours, data = example_df)

# Generate the effects plot for the interaction term
library(effects)
effects_plot_example <- plot(allEffects(models_example, data = example_df))

# Display the plot
print(effects_plot_example)

enter image description here

Examples of what I tried:

  1. I attempted to rename the variable to Educational Level before plotting but the space gave me this error
Error in parse(text = paste("~", paste(extras, collapse = "+"))) : 
  <text>:1:28: unexpected symbol
  1. I tried several arguments in the plot/allEffects function like panel.title but I can't find which could modify this part of the plot, I am not even sure how this part of the plot is called.

Solution

  • If you use effects:::plot.eff() directly, it will produce a plot.eff object for which you can modify the strip function which is used to create the labels you want to change.

    library(lattice)
    # save the plot object
    ef.plot <- effects:::plot.eff(allEffects(models_example, data = example_df)[[1]])
    
    # change the function which draws strips at the top of the panels to a custom one
    ef.plot$strip <- strip.custom(
      var.name = 'Educational Level', 
      factor.levels = c('College', 'Graduate', 'High School'),
      strip.names = c(T, T),
      strip.levels = c(T, T),
      sep = ': ')
    
    # draw the plot with the new strip function
    ef.plot
    

    effects plot with modified labels