2

In the ggplot2 examples, one finds a lot of code with two points used around parameters. For instance, in the stat_density documentation:

m <- ggplot(movies, aes(x=rating, weight=votes/sum(votes)))
m + geom_histogram(aes(y=..density..)) + geom_density(fill=NA, colour="black")

What does ..something.. mean? How can it be used? Is there any mention of it in the documentation? I am having trouble finding any reference to this due to the difficult searchability of ..

YuppieNetworking
  • 8,672
  • 7
  • 44
  • 65

1 Answers1

3

something is a new variable that has been produced by a stat, which is a ggplot2 mechanism that will transform your original dataset in some way (e.g., binning the data, smoothing the data). The .. distinguishes it from variables in your input, so that there's no confusion.

In your example, ..density.. is the density, which you can map the height of the histogram bars to, rather than the raw count in each bin (..count.., the default). ..density.. is computed by stat_bin.

As far as I know, there's no one place in the documentation where this is explained (though if you have access to the ggplot2 book, look at section 4.7), but the new variables created by each stat are listed in the stat documentation pages, under the Value section. For example, looking at the documentation for stat_bin, you can see that the variables count, density, ncount, and ndensity are created, which can be accessed by ..count.., ..density.., ..ncount.., and ..ndensity...

Peyton
  • 7,266
  • 2
  • 29
  • 29