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

如何在ggplot2箱线图中分组连接不同组的平均值

问题:在ggplot2中分别连接WT组和at5g01950组的平均值

我需要在现有ggplot2图形中,分别连接蓝色组(WT)和红色组(at5g01950)的平均值。以下是我的相关信息:

数据集

structure(list(G = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L), levels = c("1", "2", "3", "4", "5", "6"), class = "factor"), 
    T = c(19, 19, 19, 20, 18, 20, 16, 17, 17, 18, 17, 19, 17, 
    19, 17, 16, 15, 18, 15, 15, 15, 16, 15, 16, 16, 16, 15, 14, 
    16, 15, 25, 25, 25, 26, 21, 25, 23, 22, 23, 23, 24, 21, 23, 
    24, 23, 24, 22, 23, 22, 21, 22, 22, 20, 22, 22, 23, 21, 21, 
    22, 21, 40, 41, 41, 41, 41, 41, 37, 37, 39, 40, 40, 39, 38, 
    39, 37, 40, 37, 38, 35, 36, 36, 36, 35, 35, 37, 38, 36, 36, 
    37, 35)), row.names = c(NA, -90L), class = c("tbl_df", "tbl", 
"data.frame"))

当前使用的绘图代码

ggplot(TempFlor2, aes(x = G, y = T, 
                               color = c('WT', 'at5g01950')[2 - (as.numeric(G) %% 2)])) +
    geom_jitter(position = position_jitter(width = 0.2, height = 0), alpha = 0.5) +
    geom_boxplot(width = 0.5, color = "black", alpha = 0.2, 
                 position = position_dodge(0.9)) + 
    theme_minimal() + 
    theme(plot.background = element_rect(fill = "white") ,
          panel.grid.minor = element_blank()) +
    labs(color = '')

尝试过的无效代码

ggplot(TempFlor2, aes(x = G, y = T, 
                 color = c('WT', 'at5g01950')[2 - (as.numeric(G) %% 2)])) +
    geom_jitter(position = position_jitter(width = 0.2), alpha = 1) +
    scale_color_manual(values = c(WT = '#3CBEC3', at5g01950 = '#C3413C')) +
    geom_boxplot(width = 0.5, color = "black", alpha = 0.2, 
                 position = position_dodge(0.9)) + 
    stat_summary(fun.y = median, geom = 'line', aes(group = G, colour = G))
theme_minimal() + 
    theme(plot.background = element_rect(fill = "white") ,
          panel.grid.minor = element_blank()) +
    labs(color = '')

之前的尝试要么无法运行,要么会错误连接所有点,无法区分WT和at5g01950两组。


正确实现方案

关键是先给数据集添加明确的分组列,让ggplot能正确识别分组逻辑,再基于分组连接平均值:

完整代码

library(ggplot2)
library(dplyr)

# 先给数据添加明确的Group列
TempFlor2 <- TempFlor2 %>%
  mutate(Group = case_when(
    as.numeric(G) %% 2 == 1 ~ "WT",
    TRUE ~ "at5g01950"
  ))

ggplot(TempFlor2, aes(x = G, y = T, color = Group)) +
  # 散点
  geom_jitter(position = position_jitter(width = 0.2, height = 0), alpha = 0.5) +
  # 箱线图
  geom_boxplot(width = 0.5, color = "black", alpha = 0.2, 
               position = position_dodge(0.9)) +
  # 连接每组的平均值(关键:group = Group,确保按组别连线)
  stat_summary(
    fun = mean, 
    geom = "line", 
    aes(group = Group),
    linewidth = 1
  ) +
  # 手动设置颜色
  scale_color_manual(values = c(WT = '#3CBEC3', at5g01950 = '#C3413C')) +
  # 主题设置
  theme_minimal() + 
  theme(
    plot.background = element_rect(fill = "white"),
    panel.grid.minor = element_blank()
  ) +
  labs(color = '')

代码说明

  1. 添加分组列:用mutate和case_when生成Group列,明确每个样本属于WT还是at5g01950,避免在aes中临时生成分组导致的识别问题。
  2. 连线逻辑:stat_summary中指定group = Group,让ggplot按照WT和at5g01950两个组别分别连接对应x轴位置的平均值。
  3. 颜色映射:通过scale_color_manual指定两组的颜色,和需求一致。

内容的提问来源于stack exchange,提问作者André Lima

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 11:37:04