View source: R/grobs-complex.r
| gg2density | R Documentation | 
Perform a 2D kernel density estimatation using kde2d and
display the results with contours.
gg2density(plot, aesthetics = list(), ..., data = NULL)
| plot | the plot object to modify | 
| aesthetics | named list of aesthetic mappings, see details for more information | 
| ... | other options, see details for more information | 
| data | data source, if not specified the plot default will be used | 
This is another function useful for dealing with overplotting.
Aesthetic mappings that this grob function understands:
x:x position (required)
y:y position (required)
These can be specified in the plot defaults (see ggplot) or
in the aesthetics argument.  If you want to modify the position
of the points or any axis options, you will need to add a position scale to
the plot.  These functions start with ps, eg.
pscontinuous or pscategorical
Other options:
 passed to ggcontour, see it for details
ggcontour for another way of dealing with over plotting
library(ggplot2movies)
m <- ggpoint(ggplot(movies, aesthetics=list(y=length, x=rating)))
dens <- MASS::kde2d(movies$rating, movies$length)
densdf <- data.frame(expand.grid(rating = dens$x, length = dens$y), z=as.vector(dens$z))
ggcontour(m, list(z=z), data=densdf)
gg2density(m)
# they don't look the same due to scaling effects on kde2d
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.