0

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.

  • This should help: https://stackoverflow.com/questions/6644997/showing-data-values-on-stacked-bar-chart-in-ggplot2. You want to use something like `geom_text(size = 3, position = position_stack(vjust = 0.5))` – thehand0 Feb 12 '22 at 21:51

1 Answers1

0

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")
TarJae
  • 72,363
  • 6
  • 19
  • 66
  • Thanks for the attempt! Unfortunately, it's not working as it shows me columns made of values instead of the necessary bars. It doesn't show the percentage. I need somehow to filter the values by condition and period and then to calculate percentages. However, it's hard for me to do in R. – Anel Kaliyeva Feb 13 '22 at 13:04