Background
I'm looking at a possibility of using special variables (..count.., etc.)
while adjusting elements of a histogram.
Example
For instance the code below generates a simple histogram with the bars stretched to the maximum length of the panel and the neat margin ot the top achieved through limits = c(0,15)
.
require(ggplot2)
ggplot(mtcars) +
geom_histogram(aes(x = cyl)) +
scale_y_continuous(expand = c(0,0),
limits = c(0,15))
Preview
Problem
My intention is to make the limits = c(0,15)
dynamic and connect to the maximum available count without the necessaity to generate a separate data frame. Consequtntly, I would like to make use of the special variables, for example, I would like to add 10% margin on top with use of the code:
ggplot(mtcars) +
geom_histogram(aes(x = cyl)) +
scale_y_continuous(expand = c(0,0),
limits = c(0, ..count.. * 1.1))
however, this results in an error:
Error in continuous_scale(c("y", "ymin", "ymax", "yend", "yintercept", :
object '..count..' not found
I understand that ..count..
should be available as it's created by stat_bin
called by histogram, as per Brian's answer.