简体   繁体   中英

How display percent values on a stacked bar plot using ggplot in R

I've produced this graph (on the attached photo) using ggplot2.

My code goes like this:

ggplot(data, aes(fill=condition, y=value, x=period)) + 
geom_bar(position="fill", stat="identity")+
xlab("Période") +  ylab("Pourcentage") +
ggtitle("Répartition des français et étrangers")+
scale_y_continuous(labels = scales::percent)+
scale_fill_brewer(palette = "OrRd")

Result looks like this

I just need to display the values on the bars. However, adding geom_text is not straightforward. I would appreciate it if you could provide some ideas.

Without knowing the data:

Try: geom_text(aes(label = paste0(value*100,"%")), position = position_stack(vjust = 0.5), size = 2)

ggplot(data, aes(fill=condition, y=value, x=period)) + 
  geom_bar(position="fill", stat="identity")+
  geom_text(aes(label = paste0(value*100,"%")), 
            position = position_stack(vjust = 0.5), size = 2)
  xlab("Période") +  ylab("Pourcentage") +
  ggtitle("Répartition des français et étrangers")+
  scale_y_continuous(labels = scales::percent)+
  scale_fill_brewer(palette = "OrRd")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM