You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用plot_model按交互项为数据点着色时遇离散值错误排查

问题原因分析
  • 核心错误:geom_point的colour参数用单引号包裹了变量名,导致R把'Financial_Open_Logged'当作一个离散的字符串常量,而非数据中的连续型变量Financial_Open_Logged,后续调用scale_color_continuous()时,连续刻度无法匹配离散值,触发报错。
  • 次要问题:plot_model(type="int")默认已经为交互项线条添加了颜色刻度,额外添加scale_color_continuous()会覆盖原有刻度并触发警告。
修正后的代码
plot_model(model, 
           type = "int", 
           terms = c("p95", "Financial_Open_Logged"), 
           show.data = FALSE) + 
  geom_point(data = data_master1, 
             aes(x = p95, 
                 y = dgentav14, 
                 colour = Financial_Open_Logged),  # 去掉单引号,引用真实变量
             inherit.aes = FALSE)
可选优化(自定义颜色刻度)

如果需要手动调整连续颜色的样式,可以保留scale_color_continuous()并设置参数,同时指定自定义图例名称避免冲突:

plot_model(model, 
           type = "int", 
           terms = c("p95", "Financial_Open_Logged"), 
           show.data = FALSE) + 
  geom_point(data = data_master1, 
             aes(x = p95, 
                 y = dgentav14, 
                 colour = Financial_Open_Logged),  
             inherit.aes = FALSE) +
  scale_color_continuous(name = "金融全球化程度",  # 自定义图例名称
                         low = "#1f77b4", 
                         high = "#ff7f0e")

内容的提问来源于stack exchange,提问作者user21581187

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 13:22:15