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

如何在ggplot中设置geom_bar按因子着色、geom_jitter填充白色?

解决方案

问题出在你把fill = cellobiose放在了全局aes()中,导致geom_jitter继承了这个填充色设置。只需调整填充色的作用范围,就能实现需求:

  • 将全局aes()中的fill参数移除,只保留x和y轴映射
  • 在geom_bar的局部aes()中添加fill = cellobiose,让柱状图单独使用该填充规则
  • 给geom_jitter直接指定fill = "white",覆盖继承的填充设置

修改后的完整代码:

ggplot(dataframe, 
       aes(x = `glucose`, y = `CTV dilution factor`))+
  geom_bar(aes(fill = `cellobiose`), stat = "summary", position = "dodge")+
  geom_jitter(position = position_jitterdodge(), 
              shape = 21, stroke=0.5, size=2.5, fill = "white", show.legend = FALSE)+
  ylab("CTV dilution factor")+
  xlab("glucose [mM]")+
  facet_wrap(~condition, ncol=2)+
  theme_manish()+
  theme(text=element_text(size=16))

这样修改后,柱状图会根据cellobiose因子填充对应颜色,而散点的填充色固定为白色,同时散点依然会通过position_jitterdodge()和柱状图对齐。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 19:07:47