0

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

Initial histogram

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.

Community
  • 1
  • 1
Konrad
  • 17,740
  • 16
  • 106
  • 167
  • 1
    Are you opposed to using `limits = c(0, max(table(mtcars$cyl)) * 1.1))`? – Zach Oct 18 '16 at 13:51
  • 2
    Or, as an alternative to `..count.. <- max(table(mtcars$cyl))`, sth like `ggplot(mtcars) + geom_histogram(aes(x = cyl)) -> p; p + scale_y_continuous(expand = c(0,0), limits = c(0, max(ggplot_build(p)$data[[1]]$count) * 2.1))`. – lukeA Oct 18 '16 at 13:54
  • @Zach Thanks for showing the interest, if push comes to shove I could live with this but here I want to make use of the `..count..` partially because I never use those things partially because the actual shart is part of a longis dplyr manipulation and actual data frame would be more complex than `table(mtcars$cyl)`, in practice `..count..` would solve it. – Konrad Oct 18 '16 at 13:54
  • `..count..` is only available within the `geom_histogram` aesthetics (in this case). It is not available anywhere else (as far as I know) including other geoms and stats, scales etc. One possible reason is that one can easily add multiple geoms that use `stat_count`, and it is unclear to which count `..count..` should refer. – Axeman Oct 18 '16 at 15:05
  • @Axeman Thanks very much for that input, this is useful and informative. Your explanation makes sense, shame that there is no way to fish it out, `geom_histogram()$..count..` but that would be comparable to solution that *Zach* and *lukeA* offered. – Konrad Oct 18 '16 at 15:08
  • 1
    @Konrad; Yes, fishing it out would involve `ggplot_build` as lukeA shows. – Axeman Oct 18 '16 at 15:10
  • 1
    The only truly neat solution to this actual problem would be that ggplot starts supporting unsymmetrical `expand` arguments somehow. But that seems unlikely. – Axeman Oct 18 '16 at 15:11
  • @Axeman If you wish to collate your comments in an answer I would be happy to accept as it closes the discussion. – Konrad Oct 18 '16 at 15:16

0 Answers0