如何在ggplot条形图中调整各条形块的标签至居中位置?
问题描述
我有如下数据集,想要绘制以AA为y轴、FCXM_no为x轴的条形图,要求每个条形块内的Specificity_uniqueaa标签居中对齐。但我编写的ggplot代码无法实现标签居中,请求调整代码。
数据集dput代码:
dput(FCXM_AA_sp) structure(list(Specificity_uniqueaa = structure(1:54, levels = c("A3_D161", "A*30:01_W152", "B57_V97", "Cw5_K138", "Cw9_R91", "B13_L145", "A*11:02_K19", "B18_G30", "A2_H74", "B63_F4", "B44_V199", "Cw16_L193", "DR8_L74", "DQ4_D71", "A80_S31", "A80_E56", "A80_N74", "A80_K253", "A80_K255", "Cw1_K6", "Cw1_C99", "Cw1_M248", "Cw7_L194", "Cw7_M261", "Cw7_S273", "DR7_K14", "DR7_Q25", "DR7_L30", "DR10_E10", "DR10_R30", "DR10_V31", "DQ2_S28", "DQ2_S30", "DQ2_I37", "DQ2_L52", "DQB1*06:01_P3", "DQB1*06:01_L9", "DR4_H13", "DR4_H33", "DR4_Y96", "DR51_Q9", "DR51_Q191", "DR53_A11", "DR53_C13", "DR53_L18", "DR53_W25", "DR53_N26", "DR53_I28", "DR53_N41", "DR53_Q48", "DR53_Y81", "DR53_Q187", "DQ5_L14", "DQ5_S125"), class = "factor"), AA = structure(c(17L, 2L, 5L, 14L, 15L, 4L, 14L, 10L, 13L, 1L, 5L, 4L, 4L, 17L, 12L, 16L, 19L, 14L, 14L, 14L, 8L, 7L, 4L, 7L, 12L, 14L, 18L, 4L, 16L, 15L, 5L, 12L, 12L, 3L, 4L, 9L, 4L, 13L, 13L, 11L, 18L, 18L, 6L, 8L, 4L, 2L, 19L, 3L, 19L, 18L, 11L, 18L, 4L, 12L), levels = c("F", "W", "I", "L", "V", "A", "M", "C", "P", "G", "Y", "S", "H", "K", "R", "E", "D", "Q", "N"), class = "factor"), FCXM = c("Negative", "Negative", "Positive", "Negative", "Negative", "Negative", "Negative", "Positive", "Positive", "Negative", "Negative", "Negative", "Negative", "Negative", "Negative", "Negative", "Negative", "Negative", "Negative", "Positive", "Positive", "Positive", "Negative", "Negative", "Negative", "Positive", "Positive", "Positive", "Positive", "Positive", "Positive", "Positive", "Positive", "Positive", "Positive", "Negative", "Negative", "Positive", "Positive", "Positive", "Negative", "Negative", "Negative", "Negative", "Negative", "Negative", "Negative", "Negative", "Negative", "Negative", "Negative", "Negative", "Negative", "Negative"), FCXM_no = c(-1L, -1L, 1L, -1L, -1L, -1L, -1L, 1L, 1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, 1L, 1L, 1L, -1L, -1L, -1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, -1L, -1L, 1L, 1L, 1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L)), row.names = c(NA, -54L), class = "data.frame")
期望效果:每个AA对应的条形块(按FCXM正负区分)内,Specificity_uniqueaa标签居中显示,条形图以AA为y轴、FCXM_no为x轴。
现有ggplot代码:
ggplot(FCXM_AA_sp, aes(y = FCXM_no, x = AA, fill = FCXM)) + # Fill column geom_bar(stat = "identity", width = .6, color= "white", size=0.5) + # draw the bars geom_text(aes(label=paste0(Specificity_uniqueaa,"")),nudge_y=10,colour = "black", size = 4, hjust = 8.4, vjust = 1) + #angle=90 scale_y_discrete() + # Labels scale_x_discrete() + # Labels coord_flip() + # Flip axes labs(title="FCXM vs AA") + theme_tufte() + # Tufte theme from ggfortify theme(plot.title = element_text(hjust = .5), axis.ticks = element_blank()) + # Centre plot title scale_fill_brewer(palette = "Dark2") # Color palette
解决方案
原代码的问题在于使用了固定的nudge_y和不合理的hjust值,导致标签无法自动适配条形块位置。我们可以通过position_stack(vjust = 0.5)让文本自动居中于每个条形块内,同时调整轴映射的顺序(避免混淆coord_flip后的轴方向)。
修改后的代码:
library(ggplot2) library(ggthemes) # 需要加载ggthemes以使用theme_tufte ggplot(FCXM_AA_sp, aes(x = AA, y = FCXM_no, fill = FCXM)) + geom_bar(stat = "identity", width = .6, color = "white", size = 0.5) + # 关键:用position_stack(vjust=0.5)让标签在条形块内垂直居中 geom_text(aes(label = Specificity_uniqueaa), position = position_stack(vjust = 0.5), colour = "black", size = 4) + coord_flip() + labs(title = "FCXM vs AA") + theme_tufte() + theme(plot.title = element_text(hjust = 0.5), axis.ticks = element_blank()) + scale_fill_brewer(palette = "Dark2")
关键调整说明:
- 调整了
aes中的轴映射顺序:先写x=AA、y=FCXM_no,再通过coord_flip转成y轴为AA、x轴为FCXM_no,逻辑更清晰。 - 移除了固定偏移的
nudge_y和不合理的hjust参数,改用position_stack(vjust = 0.5),让ggplot自动计算每个条形块的中心位置并放置标签。 - 移除了冗余的
scale_y_discrete()和scale_x_discrete(),ggplot会自动识别离散型轴并处理。
内容的提问来源于stack exchange,提问作者Pawan Raghav
相关产品推荐
相关产品推荐

