6

I don't understand what the . in the following code is doing or where to find documentation for it:

library(tidyverse)

ggplot(iris) + 
  geom_point(
    aes(x=Sepal.Length, y=Sepal.Width), 
    data = . %>% filter(Species == 'setosa')
  )

This appears to be behaving quite differently from the usage described in What does the dplyr period character "." reference? where the . does not appear in the left-hand-most position.

The docs here say merely

A pipeline with a dot (.) as LHS will create a unary function. This is used to define the aggregator function.

but this is not at all clear to me and I'm hoping for more information.

nicolaskruchten
  • 26,384
  • 8
  • 83
  • 101
  • It's the previous data.frame. – Rui Barradas Nov 22 '18 at 18:31
  • I'm not sure why that would be or where the documentation is? – nicolaskruchten Nov 22 '18 at 18:40
  • Documentation? Here we go: [magrittr v1.5](https://www.rdocumentation.org/packages/magrittr/versions/1.5), [vignette](https://cran.r-project.org/web/packages/magrittr/vignettes/magrittr.html). Cheers – Henrik Nov 22 '18 at 18:41
  • The only explanation in those docs is "A pipeline with a dot (.) as LHS will create a unary function. This is used to define the aggregator function." ... i was hoping for MORE information by asking this question... – nicolaskruchten Nov 22 '18 at 19:11
  • it seems like it means that it will act like `function(x) {x}` is that right? – nicolaskruchten Nov 22 '18 at 19:21
  • By `identity` and `identity()` do not work in this place. Not worthy of its own question/answer here? certainly neither of the linked "dupes" elucidate this. – nicolaskruchten Nov 22 '18 at 19:26

1 Answers1

5

The confusion here can actually come from two places.

First, yes, the . %>% something() syntax creates a "unary" function that takes one argument. So:

. %>% filter(Species == 'setosa')

is equivalent to

function(.) filter(., Species == 'setosa')

The second part here is that ggplot2 layers can actually take a function as their data argument. From e.g. ?geom_point:

The data to be displayed in this layer. There are three options:

...

A function will be called with a single argument, the plot data. The return value must be a data.frame, and will be used as the layer data.

So the function that is passed to geom_point will always be applied to the default plot data (i.e. the data defined in ggplot()).

Note that your linked question concerns the use of . in funs(), which is not directly related to it's use here.

Community
  • 1
  • 1
Axeman
  • 32,068
  • 8
  • 81
  • 94
  • Thanks! So is it fair to say that the `.` by itself is equivalent to `function(x){x}` in this context? – nicolaskruchten Nov 23 '18 at 20:41
  • `.` by itself does nothing, it's the `%>%` that recognizes it and does all the magic. So you really `. %>% something` as a combination for a function to be created. You can look at the source of the pipe and see it explicitly checks if the left hand side (LHS) is a placeholder (i.e. `.`), so it is programmed as a special case. – Axeman Nov 23 '18 at 21:07