如何去除ggplot中第二个图例标题?glmer绘图问题排查
解决glmer模型预测图的图例异常与虚线来源问题
问题说明
用ggpredict绘制glmer模型的预测结果时,遇到两个问题:
- 图例异常:要么出现两个重复图例,要么图例没有颜色(疑似重复设置颜色编码导致)
- 不清楚图中虚线的来源
用户原始代码
plot2d = ggpredict(m2d, terms = c("Verbstellung", "group")) plot(plot2d, connect_lines = TRUE, colors = c("red", "blue")) + aes(linetype = .data[["group"]]) + ggeasy::easy_center_title()+ ggtitle("Accuracy in Test Session \n Acceptable Sentences only") + theme(plot.title = element_text(size = 18, face = "bold")) + xlab("Verb Position") + ylab("Accuracy in Percent")+ easy_all_text_size(15)+ easy_all_text_color("black")+ scale_color_manual(name = "Group", values = c("red", "blue"), labels = c("LR", "NLR"))
示例数据
group subj Verbstellung answer.corr Set actor2 nlr 38 V1 1 2 typical nlr 38 V1 0 3 typical nlr 38 V1 1 1 typical nlr 38 V2 1 3 typical nlr 38 V3 0 3 typical nlr 38 V3 1 1 typical nlr 38 V3 1 2 typical nlr 38 V2 1 1 typical lr 35 V1 0 1 typical lr 35 V3 1 2 typical lr 35 V2 1 3 typical lr 35 V2 0 1 typical lr 35 V3 0 1 typical lr 35 V1 1 3 typical lr 35 V3 1 3 typical lr 35 V1 1 2 typical
所用glmer模型
m2d = glmer(answer.corr ~ group + actor2 + Verbstellung + group:actor2 + group:Verbstellung + (1|subj) + (1|Set), data=data, family="binomial", control = glmerControl(optimizer="nlopt", optCtrl=list(maxfun=1e5), calc.derivs = FALSE))
模型输出
Generalized linear mixed model fit by maximum likelihood (Laplace Approximation) ['glmerMod'] Family: binomial ( logit ) Formula: answer.corr ~ group + actor2 + Verbstellung + group:actor2 + group:Verbstellung + (1 | subj) + (1 | Set) Data: data_akz AIC BIC logLik deviance df.resid 4033.361 4101.507 -2006.680 4013.361 6722 Random effects: Groups Name Std.Dev. subj (Intercept) 1.5788 Set (Intercept) 0.1448 Number of obs: 6732, groups: subj, 53; Set, 3 Fixed Effects: (Intercept) group1 actor21 VerbstellungV2 VerbstellungV3 group1:actor21 group1:VerbstellungV2 group1:VerbstellungV3 2.3894 0.5053 0.4712 0.4055 0.7303 0.1013 -0.3174 -0.5449
问题原因
图例异常:
plot(plot2d, colors = c("red", "blue"))已经自动为group变量绑定了颜色映射- 后续手动添加
aes(linetype = group)和scale_color_manual,相当于重复定义颜色映射规则,导致ggplot生成冲突的图例,出现重复或无颜色的情况
虚线来源:
- 图中的虚线是模型预测值的置信区间(Confidence Interval),是
ggpredict绘图时默认添加的,用来展示预测结果的不确定性范围
- 图中的虚线是模型预测值的置信区间(Confidence Interval),是
修正后的代码
直接基于ggpredict返回的预测数据手动构建ggplot,避免自动绘图的冲突:
library(ggplot2) library(ggeasy) # 获取预测数据 plot2d = ggpredict(m2d, terms = c("Verbstellung", "group")) # 手动绘制ggplot ggplot(plot2d, aes(x = x, y = predicted, color = group, linetype = group)) + # 绘制预测线 geom_line(linewidth = 1) + # 绘制置信区间(虚线,可根据需要删除) geom_ribbon(aes(ymin = conf.low, ymax = conf.high, fill = group), alpha = 0.2, linetype = "dashed") + # 统一设置颜色、线型和填充色,合并图例 scale_color_manual(name = "Group", values = c("red", "blue"), labels = c("LR", "NLR")) + scale_linetype_manual(name = "Group", values = c("solid", "dashed"), labels = c("LR", "NLR")) + scale_fill_manual(name = "Group", values = c("red", "blue"), labels = c("LR", "NLR")) + # 标题与标签设置 ggtitle("Accuracy in Test Session \n Acceptable Sentences only") + xlab("Verb Position") + ylab("Accuracy in Percent") + # 主题调整 easy_center_title() + theme(plot.title = element_text(size = 18, face = "bold")) + easy_all_text_size(15) + easy_all_text_color("black")
代码说明
- 统一在
aes中把color和linetype绑定到group,确保图例自动合并为一个 - 用
scale_*_manual系列函数统一设置图例的名称、颜色/线型/填充色,避免映射冲突 - 如果不需要置信区间,直接删除
geom_ribbon部分即可 - 手动绘图比
ggpredict的plot()方法更灵活,能完全控制所有绘图元素
内容的提问来源于stack exchange,提问作者afiw
相关产品推荐
相关产品推荐

