Label position within cumulative geom_bar

12 Views Asked by At

I would like to have the position of the labels at the right place within each colour section and in each bar of the plot. In the actual plot I would like to have the 23.1% in the red part of the bar. I did not find any solution for cumulative bar_plot (position=fill) in previous answers

Here is my dataframe:

dat_barplot2 
           p_cat Outbreak   prop_id
1      (0,0.001]       no 0.9559322
2      (0,0.001]      yes 0.0440678
3 (0.001,0.0025]       no 0.6551724
4 (0.001,0.0025]      yes 0.3448276
5 (0.0025,0.005]       no 0.2307692
6 (0.0025,0.005]      yes 0.7692308
7 (0.005,0.0075]       no 0.0000000
8 (0.005,0.0075]      yes 1.0000000`

And here is my code for the barplot:

plot2<- ggplot(data=dat_barplot2 %>% arrange(desc(p_cat),Outbreak),
               aes(y=prop_id,x=p_cat))+
  geom_col(aes(fill =Outbreak),position = "fill")+
  scale_y_continuous(labels = scales::percent)+
  scale_x_discrete(limits=rev,
                    labels=c("]0.005,0.0075]",
                            "]0.0025,0.005]",
                            "]0.001,0.0025]",
                            "]0,0.001]"))+
  labs(x="Risk categories", y="Proportion of destinations")+
   geom_text(aes(label=paste(round(prop_id*100,1),"%")), 
             vjust=1.5, color="white", size=4, position = position_dodge(width = .9))+
  theme_classic()

It gives this plot: enter image description here

0

There are 0 best solutions below