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.
sample_data = tibble(x=rnorm(1000), y=rnorm(1000)) ggplot(sample_data, aes(x=x, y=y)) + geom_point()
ggplot(sample_data, aes(x=x, y=y)) + geom_point(stat="density_2d")
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
geom_point(). I'm just trying to understand what exactly
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_boxplot() etc) do in general?