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

如何让雨云图(Raincloud Plot)中的点与分组颜色匹配?

解决Raincloud Plot点颜色无法按分组匹配的问题

你的问题出在stat_dots的设置上:代码里直接固定了colour="black",且没有将颜色与分组变量Species做映射,导致所有点只能是统一颜色,无法和分组对应。

修改方案

有两种常见调整方式,根据需求选择:

方式1:点的颜色直接匹配分组

移除stat_dots里的固定colour参数,在aes中添加colour = Species,让点的颜色随分组变化:

iris %>%
  group_by(Species) %>%
  mutate(mean = mean(Petal.Length),
         se = sd(Petal.Length)/sqrt(length(Petal.Length))) %>%
  ungroup() %>%
  ggplot(aes(x=Petal.Length, y=Species)) +
  stat_slab(aes(fill = Species)) +
  stat_dots(aes(colour = Species), side = "bottom", shape = 16) +  # 调整此处
  scale_fill_brewer(palette = "Set1") +
  scale_colour_brewer(palette = "Set1") +  # 新增,让点颜色和填充色用同调色板
  stat_summary(fun= "mean", geom= "point", colour= "black", 
               position = position_dodge(width = 0.75)) +
  theme_bw() +
  theme(legend.position = "top")+
  labs(title="Raincloud plot with ggdist")

方式2:点填充色匹配分组,保留黑色轮廓

如果想要点的轮廓是黑色,填充色随分组变化,可改用带填充的点形状(比如shape=21),分别设置fill和colour:

iris %>%
  group_by(Species) %>%
  mutate(mean = mean(Petal.Length),
         se = sd(Petal.Length)/sqrt(length(Petal.Length))) %>%
  ungroup() %>%
  ggplot(aes(x=Petal.Length, y=Species)) +
  stat_slab(aes(fill = Species)) +
  stat_dots(aes(fill = Species), side = "bottom", shape = 21, colour="black") +  # 调整此处
  scale_fill_brewer(palette = "Set1") +
  stat_summary(fun= "mean", geom= "point", colour= "black", 
               position = position_dodge(width = 0.75)) +
  theme_bw() +
  theme(legend.position = "top")+
  labs(title="Raincloud plot with ggdist")

说明

  • 方式1中需要添加scale_colour_brewer,确保点的颜色和stat_slab的填充色使用同一调色板,视觉上更统一。
  • 方式2不需要额外添加颜色比例尺,因为fill已经和stat_slab共用了scale_fill_brewer的设置。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 09:25:26