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")