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

在R语言中为emmean结果绘制分组颜色图的方法

解决emmeans绘图按Shape分组设置不同颜色的问题

你可以通过两种方式实现按Shape(SPP/TPP)分组设置不同颜色:

方法1:直接利用emmeans的plot函数结合ggplot映射

emmeans的plot()函数底层基于ggplot,直接通过mapping参数绑定颜色与Shape变量即可:

library(emmeans)
library(ggplot2)

plot(emm, mapping = aes(color = Shape)) +
  theme_classic() +
  ylab("Shape : Item") +
  scale_color_manual(values = c("SPP" = "blue", "TPP" = "red")) # 自定义对应颜色

方法2:转为数据框后用ggplot手动绘制(更灵活)

将emm转为数据框后,可完全自定义绘图元素:

# 转换为数据框
emm_df <- as.data.frame(emm)

# 自定义ggplot绘图
ggplot(emm_df, aes(x = emmean, y = interaction(Shape, Item, sep = " : "), color = Shape)) +
  geom_point(size = 3) + # 调整点大小
  geom_errorbarh(aes(xmin = lower.CL, xmax = upper.CL), height = 0.2) + # 误差线高度
  theme_classic() +
  xlab("边际均值(Emmean)") +
  ylab("Shape : Item") +
  scale_color_manual(values = c("SPP" = "darkgreen", "TPP" = "orange"))

两种方法都能实现按Shape分组着色,方法1简洁高效,方法2适合需要调整更多绘图细节的场景。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 22:15:17