I'd like to plot histogram and density on the same plot. What I would like to add to the following is custom y-axis label which would be something like sprintf("[%s] %s", ..density.., ..count..) - two numbers at one tick value. Is it possible to obtain this with scale_y_continuous or do I need to work this around somehow?
Below current progress using scales::trans_new and sec_axis. sec_axis is kind of acceptable but the most desirable output is as on the image below.
set.seed(1) var <- rnorm(4000) binwidth <- 2 * IQR(var) / length(var) ^ (1 / 3) count_and_proportion_label <- function(x) { sprintf("%s [%.2f%%]", x, x/sum(x) * 100) } ggplot(data = data.frame(var = var), aes(x = var, y = ..count..)) + geom_histogram(binwidth = binwidth) + geom_density(aes(y = ..count.. * binwidth)) + scale_y_continuous( # this way trans = trans_new(name = "count_and_proportion", format = count_and_proportion_label, transform = function(x) x, inverse = function(x) x), # or this way sec.axis = sec_axis(trans = ~./sum(.), labels = percent, name = "proportion (in %)") ) I've tried to create object with breaks before basing on the graphics::hist output - but these two histogram differs.
bins <- (max(var) - min(var))/binwidth hdata <- hist(var, breaks = bins, right = FALSE) # hist generates different bins than `ggplot2` At the end I would like to get something like this:


