Home > OS >  Show percent in ggplot histogram
Show percent in ggplot histogram

Time:09-17

I do a histogram in percentages (which works well) but when I try to print the value of the percentages, the scale of the y axis is not right anymore.

The code for the histogram in percentages (which works well) is the following :

data_impact_final %>%
  ggplot(aes(x = time_to_treat_month))  
  geom_histogram(aes(y = (..count..)/sum(..count..)),binwidth=6)  
  scale_y_continuous(labels = scales::percent)

enter image description here

However, when I try to print the percentages on the graph using stat_bin, the scale of the y axis is not right anymore. Here is the code I use to print the percentages :

data_impact_final %>%
  ggplot(aes(x = time_to_treat_month))  
  geom_histogram(aes(y = (..count..)/sum(..count..)),binwidth=6)  
  stat_bin(binwidth=6, geom='text', color='white', aes(label = scales::percent((..count..)/sum(..count..))),position=position_stack(vjust = 0.5)) 
  scale_y_continuous(labels = scales::percent)

enter image description here

Thank you for your help

CodePudding user response:

The issue is that the labels are placed at y=..count... To solve your issue use y=..count../sum(..count..) in stat_bin too.

Making use of ggplot2::mpg as example data:

library(ggplot2)
library(dplyr)

mpg %>%
  ggplot(aes(x = hwy))  
  geom_histogram(aes(y = (..count..)/sum(..count..)),binwidth=6)  
  scale_y_continuous(labels = scales::percent)

mpg %>%
  ggplot(aes(x = hwy))  
  geom_histogram(aes(y = (..count..)/sum(..count..)),binwidth=6)  
  stat_bin(binwidth=6, geom='text', color='white', aes(y = ..count../sum(..count..), label = scales::percent((..count..)/sum(..count..))),position=position_stack(vjust = 0.5)) 
  scale_y_continuous(labels = scales::percent)

  • Related