我想在 ggplot2 中创建一个堆积条形图,其中数据标签位于与其相关的填充区域的中心。我尝试过的代码的标签顺序与应有的顺序相反。这是代码示例:
data_rep <- data.frame(Task.Number = c('5.004','5.004','5.01','5.01','5.04','5.04'),
Within_SLA = rep(c('No','Yes'),3),
Perc_SLA = c(4.8,95.2,1,99,9.6,90.4))
ggplot(data_rep, aes(x=Task.Number,y=Perc_SLA)) +
geom_bar(aes(fill=Within_SLA),stat="identity",position="stack") +
geom_text(aes(label=Perc_SLA), size = 3) +
scale_fill_discrete(name = "Within SLA") +
coord_flip()
这就是我得到的:
您还需要设置文本标签的堆叠位置。
ggplot(data_rep, aes(x=Task.Number,y=Perc_SLA, fill=Within_SLA)) +
geom_bar(stat="identity", position="stack") +
geom_text(aes(label=Perc_SLA), position =position_stack(vjust = 0.5)) +
scale_fill_discrete(name = "Within SLA") +
coord_flip()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)