KateS
KateS

Reputation: 129

Show percent in ggplot histogram

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

Upvotes: 8

Views: 12176

Answers (1)

stefan
stefan

Reputation: 124138

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

UPDATE As of ggplot2 3.4.0 the dot-dot-noation (..count..) was deprecated in favor of after_stat().

Making use of ggplot2::mpg as example data:

library(ggplot2)
library(dplyr)

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

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

Upvotes: 9

Related Questions