如何在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
相关产品推荐
相关产品推荐

