I've been struggling to understand what exactly `stat_...()`

functions do for quite a while, yet without a clear answer. Let me start with an example code.

Code 1:

```
sample_data = tibble(x=rnorm(1000), y=rnorm(1000))
ggplot(sample_data, aes(x=x, y=y)) + geom_point()
```

Code 2:

```
ggplot(sample_data, aes(x=x, y=y)) + geom_point(stat="density_2d")
```

Code 3:

```
ggplot(sample_data, aes(x=x, y=y)) + geom_point(stat="density")
#Error: geom_point requires the following missing aesthetics: y
```

I know that there is no point using `stat="density_2d"`

or `stat="density"`

with `geom_point()`

. I'm just trying to understand what exactly `stat_density_2d()`

and `stat_density()`

do behind the scene. Can anyone explain why the results of Code 1 and Code 2 differ, and why Code 3 throws an error? And what `stat_...()`

functions (`stat_bin()`

, `stat_contour()`

, `stat_boxplot()`

etc) do in general?