stat_density2d

stat_density2d(mapping=NULL, data=NULL, geom="density2d", position="identity", na.rm=FALSE, contour=TRUE, ...)

Density estimation, 2D

This page describes stat_density2d, see layer and qplot for how to create a complete plot from individual components.

What do you think of the documentation? Please let me know by filling out this short online survey.

Aesthetics

The following aesthetics can be used with stat_density2d. Aesthetics are mapped to variables in the data with the aes function: stat_density2d(aes(x = var)). Note that you do not need quotes around the variable name.

Scales control how the variable is mapped to the aesthetic and are listed after each aesthetic.

Aesthetic Default Related scales
xrequiredcontinuous, date, datetime, discrete
yrequiredcontinuous, date, datetime, discrete
colour#3366FFbrewer, gradient, gradient2, gradientn, grey, hue, identity, manual
size0.5identity, manual, size
groupinteraction(..piece.., ..level..)

Layers are divided into groups by the group aesthetic. By default this is set to the interaction of all categorical variables present in the plot.

New variables produced by the statistic

To use these variables in an aesthetic mapping, you need to surrond them with .., like aes(x = ..output..). This tells ggplot that the variable isn't the original dataset, but has been created by the statistic.

Parameters

Parameters control the appearance of the stat. In addition to the parameters listed below (if any), any aesthetic can be used as a parameter, in which case it will override any aesthetic mapping.

Returns

This function returns a layer object.

Examples

> m <- ggplot(movies, aes(x=rating, y=length)) + geom_point() + scale_y_continuous( 
+ limits=c(1, 500)) 
> m + geom_density2d() 
Warning: Removed 9 rows containing missing values (stat_density2d).
Warning: Removed 9 rows containing missing values (geom_point).
  
>  
> dens <- MASS::kde2d(movies$rating, movies$length, n=100) 
> densdf <- data.frame(expand.grid(rating = dens$x, length = dens$y), z=as.vector( 
+ dens$z)) 
> m + geom_contour(aes(z=z), data=densdf) 
Warning: Removed 9000 rows containing missing values (stat_contour).
Warning: Removed 9 rows containing missing values (geom_point).
  
>  
> m + geom_density2d() + scale_y_log10() 
  
> m + geom_density2d() + coord_trans(y="log10") 
Warning: Removed 9 rows containing missing values (stat_density2d).
Warning: Removed 9 rows containing missing values (geom_point).
Error: need at least two non-NA values to interpolate
  
>  
> m + stat_density2d(aes(fill = ..level..), geom="polygon") 
Warning: Removed 9 rows containing missing values (stat_density2d).
Warning: Removed 9 rows containing missing values (geom_point).
  
>  
> qplot(rating, length, data=movies, geom=c("point","density2d"), ylim=c(1, 500)) 
Warning: Removed 9 rows containing missing values (stat_density2d).
Warning: Removed 9 rows containing missing values (geom_point).
  
>  
> # If you map an aesthetic to a categorical variable, you will get a 
> # set of contours for each value of that variable 
> qplot(rating, length, data = movies, geom = "density2d", 
+   colour = factor(Comedy), ylim = c(0, 150)) 
Warning: Removed 979 rows containing missing values (stat_density2d).
Warning: Removed 126 rows containing missing values (stat_density2d).
Error: invalid line type
  
> qplot(rating, length, data = movies, geom = "density2d", 
+   colour = factor(Action), ylim = c(0, 150)) 
Warning: Removed 864 rows containing missing values (stat_density2d).
Warning: Removed 241 rows containing missing values (stat_density2d).
Error: invalid line type
  
> qplot(carat, price, data = diamonds, geom = "density2d", colour = cut) 
Error: invalid line type
  
>  
> # Another example ------ 
> d <- ggplot(diamonds, aes(carat, price)) + xlim(1,3) 
> d + geom_point() + geom_density2d() 
Warning: Removed 34912 rows containing missing values (stat_density2d).
Warning: Removed 34912 rows containing missing values (geom_point).
  
>  
> # If we turn contouring off, we can use use geoms like tiles: 
> d + stat_density2d(geom="tile", aes(fill = ..density..), contour = FALSE) 
Warning: Removed 34912 rows containing missing values (stat_density2d).
  
> last_plot() + scale_fill_gradient(limits=c(1e-5,8e-4)) 
Warning: Removed 34912 rows containing missing values (stat_density2d).
  
>  
> # Or points: 
> d + stat_density2d(geom="point", aes(size = ..density..), contour = FALSE) 
Warning: Removed 34912 rows containing missing values (stat_density2d).
  

What do you think of the documentation? Please let me know by filling out this short online survey.