使用ggeffects添加事后检验字母时位置异常且报错,求解决方案
解决ggeffects绘图添加显著性字母的报错问题
错误原因
add.data = TRUE兼容性问题:你使用的是零膨胀Gamma模型(ziGamma),ggeffects的plot()函数自带的add.data = TRUE参数在这类模型下无法正确关联原始数据,触发"Raw data not available"警告,进而引发后续逻辑判断的缺失值错误。- 硬编码列索引不可靠:用
lt[,7]或lt[,8]提取显著性字母的方式不严谨,不同cld()结果的列数会变化,容易导致索引错位。 - 位置变量不匹配:原代码中计算的
x_1基于generation,但绘制单因素(如temp)图时,该变量与x轴分组不对应,导致字母位置错误。
修正方案
放弃ggeffects::plot()的add.data参数,手动用ggplot2添加原始数据;用列名提取显著性字母,确保与预测数据分组匹配;调整字母位置与x轴分组对齐。
修正后的完整代码
# 加载包 library(ggeffects) library(dplyr) library(glmmTMB) library(multcomp) library(lsmeans) library(ggplot2) # 读取数据并转换因子类型 ds <- read.csv("https://raw.githubusercontent.com/Leprechault/trash/main/temp_ger_ds.csv") ds$temp <- as.factor(ds$temp) ds$generation <- as.factor(ds$generation) # 拟合模型 mTCFd <- glmmTMB(development ~ temp * generation, data = ds, family = ziGamma(link = "log"))
1. 绘制temp的效应图并添加显著性字母
# 计算边际均值与显著性分组 lsm.mTCFd.temp <- lsmeans(mTCFd, "temp") lt <- cld(lsm.mTCFd.temp, Letters = letters, decreasing = TRUE) %>% mutate(temp = as.character(temp)) # 获取预测数据并匹配显著性字母 df_gg <- ggpredict(mTCFd, terms = "temp") %>% mutate(temp = as.character(x)) %>% left_join(lt %>% select(temp, .group), by = "temp") # 绘图 ggplot(df_gg, aes(x = x, y = predicted)) + geom_point(data = ds, aes(x = temp, y = development), alpha = 0.3, position = position_jitter(width = 0.1)) + geom_errorbar(aes(ymin = conf.low, ymax = conf.high), width = 0.2) + geom_point(size = 2) + geom_text(aes(label = .group), vjust = -1, size = 4) + theme_bw()
2. 绘制generation的效应图并添加显著性字母
# 计算边际均值与显著性分组 lsm.mTCFd.gera <- lsmeans(mTCFd, "generation") lt <- cld(lsm.mTCFd.gera, Letters = letters, decreasing = TRUE) %>% mutate(generation = as.character(generation)) # 获取预测数据并匹配显著性字母 df_gg <- ggpredict(mTCFd, terms = "generation") %>% mutate(generation = as.character(x)) %>% left_join(lt %>% select(generation, .group), by = "generation") # 绘图 ggplot(df_gg, aes(x = x, y = predicted)) + geom_point(data = ds, aes(x = generation, y = development), alpha = 0.3, position = position_jitter(width = 0.1)) + geom_errorbar(aes(ymin = conf.low, ymax = conf.high), width = 0.2) + geom_point(size = 2) + geom_text(aes(label = .group), vjust = -1, size = 4) + theme_bw()
3. 绘制temp与generation的交互效应图并添加显著性字母
# 计算边际均值与显著性分组 lsm.mTCFd.temp.gera <- lsmeans(mTCFd, c("temp", "generation")) lt <- cld(lsm.mTCFd.temp.gera, Letters = letters, decreasing = TRUE) %>% mutate(temp = as.character(temp), generation = as.character(generation)) # 获取预测数据并匹配显著性字母 df_gg <- ggpredict(mTCFd, terms = c("temp", "generation")) %>% mutate(temp = as.character(x), generation = as.character(group)) %>% left_join(lt %>% select(temp, generation, .group), by = c("temp", "generation")) # 绘图 ggplot(df_gg, aes(x = x, y = predicted, color = group, group = group)) + geom_point(data = ds, aes(x = temp, y = development, color = generation), alpha = 0.3, position = position_jitter(width = 0.1)) + geom_errorbar(aes(ymin = conf.low, ymax = conf.high), width = 0.2, position = position_dodge(0.5)) + geom_line() + geom_point(size = 2, position = position_dodge(0.5)) + geom_text(aes(label = .group), vjust = -1, size = 4, position = position_dodge(0.5)) + theme_bw()
内容的提问来源于stack exchange,提问作者Leprechault
相关产品推荐
相关产品推荐

