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

如何在分组箱线图上对齐geom_point并按重复设置不同符号

问题:ggplot2箱线图中用不同形状区分重复样本并对齐箱线图中心

我尝试使用ggplot2绘制按变量分组的箱线图,同时想用不同符号区分每个重复样本的数据点。

我的数据:

Cell_line<- rep(c("B1", "B2","C"), each=4)
Condition<- rep(c("1", "2", "3", "4"), times=3)
rep1 <- c(250.1202269,  NA, 87.78025978,    103.7252853,    131.3835253,    NA, 168.8831935,    135.5137408,    137.9377942, NA, 48.73955206,   73.48705161)
rep2<- c(176.5811282,   165.4414077,    58.18896416,    52.48947013,    214.1871341,    200.8850097,    312.473565, 194.1484832,    221.5290924,    208.2391158,    107.2347819,    81.38548616)
rep3 <- c(125.0917574,  71.3834596, 40.42846894, 22.41081706,   128.4170654,    114.8438056,    150.7904802,    112.1023294,    99.56769695,    135.9090866,    93.05268714,    39.17564189)

df <- data.frame(Cell_line, Condition, rep1, rep2, rep3)

使用geom_beeswarm添加点时,绘图正常:

df %>% 
  pivot_longer(cols = rep1:rep3, names_to = "replicate", values_to = "expression") %>% 
  mutate(Condition = fct_relevel(Condition, 
                                 "1", "2", "3", "4")) %>% 
  ggplot(aes(x=Condition, y=expression, colour = Cell_line))+
  geom_boxplot()+
  geom_beeswarm(dodge.width=0.75, size=2.5)

但改用geom_point设置shape参数区分replicate时,点无法对齐到对应箱线图中心,而是分散排布:

df %>% 
  pivot_longer(cols = rep1:rep3, names_to = "replicate", values_to = "expression") %>% 
  mutate(Condition = fct_relevel(Condition, 
                                   "1", "2", "3", "4")) %>% 
  ggplot(aes(x=Condition, y=expression, colour = Cell_line))+
  geom_boxplot()+
  geom_point(aes(colour=Cell_line, shape = replicate), position=position_dodge(width=1), size=3)+
  scale_shape_manual(values=c(15, 16, 17))

请问如何修改,使绘图效果与第一张图一致且能按重复设置不同符号?


解决方案

问题核心是position_dodge的分组逻辑和参数匹配问题:单纯的position_dodge无法实现组内点的分散,且若分组逻辑不统一会导致点偏移箱线图中心。改用position_jitterdodge可以同时实现对齐箱线图、组内点分散的效果,完美替代geom_beeswarm的表现。

修改后的代码如下:

library(tidyverse)
library(forcats)

df %>% 
  pivot_longer(cols = rep1:rep3, names_to = "replicate", values_to = "expression") %>% 
  mutate(Condition = fct_relevel(Condition, "1", "2", "3", "4")) %>% 
  ggplot(aes(x=Condition, y=expression, colour = Cell_line, group = Cell_line))+
  geom_boxplot(width = 0.7, position = position_dodge(width = 0.75))+
  geom_point(
    aes(shape = replicate), 
    position = position_jitterdodge(
      dodge.width = 0.75,  # 与箱线图的dodge宽度保持一致,确保对齐
      jitter.width = 0.2   # 控制组内点的分散幅度,避免重叠
    ), 
    size=3
  )+
  scale_shape_manual(values=c(15, 16, 17))

关键改动说明:

  1. 统一分组与dodge参数:全局aes中明确group = Cell_line,让箱线图和散点图的分组逻辑完全匹配;同时将箱线图的position_dodge(width)和散点的position_jitterdodge(dodge.width)设为相同数值(0.75),保证散点精准对齐箱线图中心。
  2. 使用position_jitterdodge:该位置调整器先按分组完成dodge对齐,再在组内轻微抖动点,既避免了点的重叠,又实现了和geom_beeswarm类似的分散效果。
  3. 调整箱线图宽度:设置geom_boxplot(width=0.7),让箱线图与散点的间距更协调美观。

这样修改后,就能得到和geom_beeswarm一致的对齐效果,同时用不同形状区分每个重复样本。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 14:18:12