79746770

Date: 2025-08-26 11:39:40
Score: 1
Natty:
Report link

Use scales library for percent function.

Then, in geom_histogram map y to after_stat(density) to calculate proportion instead of count.

Then add scale_y_continuous(labels = percent) to format the y-axis labels as percentages

And optionally update the y-axis title with labs(y = "Percentage")

library(ggplot2)
library(scales)

ggplot(df, aes(x = val)) + 
  geom_histogram(aes(y = after_stat(density)), 
                 colour="black", fill="white", binwidth = binwidth, size = 1.2) +
  geom_density(color="#FF6666", size = 2) +
  theme_minimal() +
  theme(axis.title = element_text(size=18),
        axis.text = element_text(size=16)) +
  scale_y_continuous(labels = percent) +
  labs(y = "Percentage")

enter image description here

Reasons:
  • Probably link only (1):
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: muramat