Home > database >  How to add percentages on top of an histogram when data is grouped
How to add percentages on top of an histogram when data is grouped

Time:02-02

This is not my data (for confidentiality reasons), but I have tried to create a reproducible example using a dataset included in the ggplot2 library. I have an histogram summarizing the value of some variable by group (factor of 2 levels). First, I did not want the counts but proportions of the total, so I used that code:

library(ggplot2)
library(dplyr)

df_example <- diamonds %>% as.data.frame() %>% filter(cut=="Premium" | cut=="Ideal")

ggplot(df_example,aes(x=z,fill=cut))   
  geom_histogram(aes(y=after_stat(width*density)),binwidth=1,center=0.5,col="black")  
  facet_wrap(~cut)  
  scale_x_continuous(breaks=seq(0,9,by=1))  
  scale_y_continuous(labels=scales::percent_format(accuracy=2,suffix=""))  
  scale_fill_manual(values=c("#CC79A7","#009E73"))  
  labs(x="Depth (mm)",y="Count")  
  theme_bw()   theme(legend.position="none")

It gave me this as a result.

enter image description here

  • Related