1
iris %>% 
ggplot(.,aes(x=Sepal.Length,y=..density..,fill=Species))+
  scale_x_continuous(limit=c(0,10),breaks =seq(0,10,1))+
  geom_density(alpha=.2)

Above script works well,but I never seen ..density.. in R.
What's ..density..?

kittygirl
  • 2,255
  • 5
  • 24
  • 52
  • 1
    It's quite old syntax that has been replaced by `after_stat(density)` in ggplot2 v3.3.0 and went through an in between phase with `stat(density)`, which is explained in the details of `?after_stat`. It just means that it is a calculated variable from the stat part of a layer. You can see these documented at stat manual pages under 'computed variables' in e.g. `?stat_density` – teunbrand Jun 01 '20 at 18:17
  • 1
    In this particular case, `..density..` (or in current ggplot `after_stat(density)`) isn't necessary. You can remove it and the plot will still work because `geom_density` is already automatically internally computing the density and placing it in the `y` aesthetic. The `.` (in `ggplot(., aes(`) also isn't necessary and can be removed, because the pipe automatically pipes the data frame into the first argument of `ggplot`. So all you need is: `iris %>% ggplot(aes(x=Sepal.Length, fill=Species)) + ...` – eipi10 Jun 01 '20 at 18:20
  • There's a short description of many of the `..var..` special symbols on the [cheat sheet](https://raw.githubusercontent.com/rstudio/cheatsheets/master/data-visualization-2.1.pdf). One of the most useful, but poorly documented ones is `..PANEL..`. – Ian Campbell Jun 01 '20 at 18:26
  • @IanCampbell huh? Never came across ..PANEL.. what does it do?? – tjebo Jun 02 '20 at 07:15
  • @Tjebo It indicates which panel of a `facet_grid` or `facet_wrap`. – Ian Campbell Jun 02 '20 at 20:11

1 Answers1

0

As teunbrand said,.. is replaced by after_stat.

after_stat() replaces the old approaches of using either stat() or surrounding the variable names with ...

kittygirl
  • 2,255
  • 5
  • 24
  • 52