有没有办法自定义plot(allEffects())面板标题?

问题描述 投票:0回答:1

我创建了下面的图来可视化交互,但我想知道我是否可以自定义图中“教育=大学教育=研究生教育=高中”的部分。

代码:

   # 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)

我尝试过的示例:

  1. 在绘图之前我尝试将变量重命名为
    Educational Level
    ,但空间给了我这个错误
Error in parse(text = paste("~", paste(extras, collapse = "+"))) : 
  <text>:1:28: unexpected symbol
  1. 我在plot/allEffects函数中尝试了几个参数,例如“panel.title”,但我找不到哪个可以修改这部分图,我什至不确定这部分图是如何调用的。
r plot graph interaction
1个回答
0
投票

如果直接使用

effects:::plot.eff()
,它将生成一个
plot.eff
对象,您可以修改该对象的
strip
函数,该函数用于创建要更改的标签。

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

© www.soinside.com 2019 - 2024. All rights reserved.