【发布时间】:2021-12-25 06:15:12
【问题描述】:
我正在使用融化的 data.table:
cat <- melt(as.data.table(insti), id.vars=c(1:3,11), measure.vars=4:10)
我用来创建情节的:
ggplot(cat,
aes(x=reorder(Llengua, -Publicacions),
y=Publicacions, fill=Xarxa))+
geom_bar(stat="identity")+#nº de publicacions
theme_classic()+
theme(axis.text.x=element_text(angle=90, hjust=0.8, vjust=0.5), legend.position="top")+
labs(x="Llengua")+
ggtitle("Catalunya")+
geom_text(aes(label =Percentatge), vjust = 0.5)+
theme(plot.title = element_text(hjust=0.5))+
scale_fill_manual(values=col.Xarxa)+
geom_hline(yintercept=0.333*sum(cat$Publicacions),
linetype="dashed", color = "dark grey")
看起来像这样: enter image description here
我的问题是:如果每一列都来自多行的总和,我如何为每一列添加百分比或绝对值标签(不考虑颜色划分)?
我在 data.table 中添加了一个 Percentatge 列(最初),因此我的 x 值 Català 和 Espanyol 具有相同的百分比:
enter image description here
但是当我尝试将标签添加到图表时,数字重复出现的次数与对条形图的贡献一样多,因此无法读取任何内容:
p + geom_text(aes(label =Percentatge), vjust = 0.5)
我可以做些什么来避免重复并在栏上设置它的位置?
【问题讨论】:
-
使用
stat_bin()参数
标签: r ggplot2 label bar-chart melt