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

如何在ggplot2并排条形图中将标签居中对齐到对应柱子上方

解决ggplot分组条形图标签未居中的问题

分组条形图的标签错位,本质是geom_bar()和geom_text()的分组偏移(dodge)宽度参数不统一,加上x轴为连续数值型变量时的宽度计算偏差导致的,按以下方式调整代码即可实现标签居中:

修改后完整代码

df<- structure(list(Cycle = c(1980, 1982, 1984, 1986, 1988, 1990, 
                         1992, 1994, 1996, 1998, 2000, 2002, 2004, 2006, 2008, 2010, 2012, 
                         2014, 2016, 1980, 1982, 1984, 1986, 1988, 1990, 1992, 1994, 1996, 
                         1998, 2000, 2002, 2004, 2006, 2008, 2010, 2012, 2014, 2016), 
               Donor_Location = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                            1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 
                                            2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
                                            2L), .Label = c("In District", "Out of District"), class = "factor"), 
               Count_Percentage_Mean = c(0.167598290871218, 0.124829496296588, 
                                         0.0940731004562636, 0.125521769946444, 0.134545184854301, 
                                         0.221904402021545, 0.241031685849158, 0.284978425863672, 
                                         0.280387277343076, 0.269095218446438, 0.273131304975636, 
                                         0.288912758780087, 0.297134465175358, 0.296786491472909, 
                                         0.292365465524159, 0.303877491711499, 0.249983664094922, 
                                         0.258636305966848, 0.239182241578899, 0.159250631842859, 
                                         0.161311478778858, 0.157209833306125, 0.161853462566676, 
                                         0.174277618030726, 0.251786815739142, 0.318324841334214, 
                                         0.312697665544327, 0.347430615609066, 0.346833684171301, 
                                         0.374139881841685, 0.422610276126137, 0.432433986875512, 
                                         0.424992976111316, 0.429557833404775, 0.443001597806887, 
                                         0.462424440669885, 0.45460402775165, 0.492540191333363)), row.names = c(NA, 
                                                                                                                 -38L), class = "data.frame")


ggplot(df, aes(x = factor(Cycle) , y = Count_Percentage_Mean, fill = Donor_Location)) +
  geom_bar(stat = "identity", position = position_dodge(width = 0.9)) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1)) +
  labs(x = "Cycle") + # 可选,将x轴名称改回Cycle,避免显示factor(Cycle)
  geom_text(aes(label=paste(round(Count_Percentage_Mean*100,0), "%", sep = "")), position=position_dodge(width=0.9), vjust=-0.5)

核心调整点说明

  • 将x轴映射的Cycle转为因子类型,避免连续数值变量导致的分组柱子宽度计算偏差;如果需要保留x轴为连续刻度,也可以不转类型,只要保证geom_bar和geom_text的position_dodge宽度参数完全一致即可
  • 显式指定geom_bar的position_dodge(width=0.9),和geom_text的偏移宽度完全匹配,即可保证标签和柱子一一对应居中

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.24 08:06:03