Special variables in ggplot (..count.., ..density.., etc.)
Asked Answered
N

2

50

Consider the following lines.

p <- ggplot(mpg, aes(x=factor(cyl), y=..count..))

p + geom_histogram()   
p + stat_summary(fun.y=identity, geom='bar')

In theory, the last two should produce the same plot. In practice, stat_summary fails and complains that the required y aesthetic is missing.

Why can't I use ..count.. in stat_summary? I can't find anywhere in the docs information about how to use these variables.

Nikaniki answered 28/1, 2013 at 20:7 Comment(1)
Those variables are returned by stat_bin which is called by geom_histogram, but not by stat_summary (since you're supposed to be supplying your own stat), hence the variables aren't available.Armandinaarmando
F
61

Expanding @joran's comment, the special variables in ggplot with double periods around them (..count.., ..density.., etc.) are returned by a stat transformation of the original data set. Those particular ones are returned by stat_bin which is implicitly called by geom_histogram (note in the documentation that the default value of the stat argument is "bin"). Your second example calls a different stat function which does not create a variable named ..count... You can get the same graph with

p + geom_bar(stat="bin")

In newer versions of ggplot2, one can also use the stat function instead of the enclosing .., so aes(y = ..count..) becomes aes(y = stat(count)).

Ferritin answered 28/1, 2013 at 20:49 Comment(4)
I'd like to perform an arithmetic operation (in this case, addition) between ..count.. variable and a user-defined variable (I've done it with constants). Unfortunately, ggplot2 doesn't recognize the variable's name and produces an error. Any ideas, Brian?Caretaker
@AleksandrBlekh That should be a new question, not a comment to the answer of an old question. Offhand, I don't think it is possible. A variable in your primary data.frame would have more values than the transformed ..count.. variable, so there would be no way to match them up. Without more detail and a reproducible example (e.g. a new question), I can't answer for sure, though.Ferritin
Fair enough! I thought that this question is too "small" and not worth a separate posting. I've already figured that it's not possible, your comment additionally confirms that. Thank you!Caretaker
I really wish geom_bar generated the ..density.. variable like its cousin function geom_histogram does.Divaricate
D
2

As part of ggplot 3.3.0, the preferred API has changed to use the after_stat() method. The following are equivalent:

aes(y=..count..)
aes(y=stat(count))
aes(y=after_stat(count))

With the release of v3.4.0, the former two have been deprecated, and after_stat() is the preferred API (see Release Notes).

There is documentation explaining the evaluation staging, but knowing what computed values are available depends on the particular layer being added. For example, the stat_bin() layer (which geom_histogram() uses) will compute:

  • count
  • density
  • ncount
  • ndensity
  • width

This info can be found in the "Computed variables" section of the method documentation. Another example is stat_count() (which geom_bar() on discrete uses) computing count and prop (see docs). So, consult the particular layer for similar info.

Designer answered 5/5, 2023 at 20:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.