geom_density_2d are linked by default – calling one will call the other. When you call call
ggplot2 creates a new plot layer using the “
density_2d” geom and the “
density2d” stat. The geom is essentially the same as the “
contour” geom, which (obviously) plots contours from the data, and the “
density2d” stat transforms the input data into density information. This creates the density contours that you see with the first bit of code.
The second bit, with
geom = "polygon", still creates a layer that uses the “
density2d” stat, but instead of the “
density_2d” geom, it uses the “
polygon” geom (normally accessed by the
geom_polygon function). That geom, unsurprisingly, creates polygons, which can then be colored different colors by the
fill aesthetic based on the
..level.. value derived by
As for what other geoms might make sense, the only ones that I’m aware of are
point, as shown in the documentation.