I am using a melted data.table:
cat <- melt(as.data.table(insti), id.vars=c(1:3,11), measure.vars=4:10)
which I used to create a plot:
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")
which looks like this: enter image description here
my question is: how can I add percentage or absolute values labels for every single column (don't take in consideration the color divisions) if every column comes from the total sum of several rows?
I added a Percentatge
column to my data.table (primitively) so my x values Català
and Espanyol
have the same percentatge:
enter image description here
but when I try to add the labels to the graph, the number appears repeated as many times as rows contributed to the bar, so nothing can be read:
p + geom_text(aes(label =Percentatge), vjust = 0.5)
what can I do to avoid the repetitions and to set its position on the bar?
Using these two posts I could answer my question:
adding overlap data label over bar chart
draw the sum value above the stacked bar
To just remove the overlaps you can use argument check_overlaps=T
in geom_text()
.
However to solve my problem I had to create a new dataframe:
m3 <- aggregate(cat$Publicacions, by=list(Llengua=cat$Llengua), FUN = sum)
m3 <- as.data.frame(m3)
and use it in geom_text
like this:
+ geom_text(aes(x=Llengua, y=x, label=x), data=m3, vjust=-0.2)