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

如何移除ggplot图表中的圆形图例?附当前代码

移除ggplot图表中的圆形图例

你的图例里出现圆形,是因为全局aes中同时设置了fill=label和colour=label——geom_errorbar会基于colour生成带圆形点的图例项,和geom_col的填充矩形图例叠加在了一起。以下两种方法可以解决这个问题:

方法1:将colour映射移至geom_errorbar的局部aes中

把colour=label从全局映射里移除,只放到geom_errorbar的aes中,这样全局只会生成fill对应的矩形图例,不会出现圆形:

ggplot(df2, aes(x = genotype, y = sum, fill = label)) +
  geom_col(position = position_dodge(), width = 0.8)+
  geom_hline(yintercept=mean(df2$sum), linetype="dashed",color = "black", linewidth=0.5)+
  geom_errorbar(aes(ymin = sum, ymax = sum + sd, colour=label),
                width = 0.4, position = position_dodge(0.9)) + 
  theme_bw()+
  scale_color_manual(values=c('#abebc6','#2ecc71','#1d8348'))+
  scale_fill_manual(labels = c('Sensitive','Moderate', 'Tolerant'),
    values = c('#abebc6','#2ecc71','#1d8348'))+
  labs(x=NULL,y=NULL)+
  scale_y_continuous(labels = number_format(accuracy = 0.1))+
  coord_cartesian(ylim = c(0, 25))+
  theme(axis.text.x = element_text(size=8,face = "bold", angle=90),
        axis.text.y = element_text(size = 8, face = "bold"),
        strip.text.y.left = element_text(face = "bold"),
        strip.placement = "outside",
        strip.background = element_blank(),
        legend.position = c(0.8, 0.95), 
        legend.direction = "horizontal",
        legend.key.size = unit(5, 'mm'),
        legend.background = element_blank(),
        legend.title = element_blank(),
        legend.text = element_text(size = 10,family = "serif",face="bold"))

方法2:直接关闭colour对应的图例

在现有代码末尾添加guides(colour = "none"),强制关闭colour生成的圆形图例,只保留fill的矩形图例:

ggplot(df2, aes(x = genotype, y = sum, fill = label,colour=label)) +
  geom_col(position = position_dodge(), width = 0.8)+
  geom_hline(yintercept=mean(df2$sum), linetype="dashed",color = "black", linewidth=0.5)+
  geom_errorbar(aes(ymin = sum, ymax = sum + sd), width = 0.4,
                position = position_dodge(0.9)) + theme_bw()+
  scale_color_manual(values=c('#abebc6','#2ecc71','#1d8348'))+
  scale_fill_manual(labels = c('Sensitive','Moderate', 'Tolerant'),
    values = c('#abebc6','#2ecc71','#1d8348'))+
  labs(x=NULL,y=NULL)+scale_y_continuous(labels = number_format(accuracy = 0.1))+
  coord_cartesian(ylim = c(0, 25))+
  theme(axis.text.x = element_text(size=8,face = "bold", angle=90),
        axis.text.y = element_text(size = 8, face = "bold"),
        strip.text.y.left = element_text(face = "bold"),
        strip.placement = "outside",
        strip.background = element_blank(),
        legend.position = c(0.8, 0.95), 
        legend.direction = "horizontal",
        legend.key.size = unit(5, 'mm'),
        legend.background = element_blank(),
        legend.title = element_blank(),
        legend.text = element_text(size = 10,family = "serif",face="bold")) +
  guides(colour = "none")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 18:03:20