stat_density

stat_density(mapping=NULL, data=NULL, geom="area", position="stack", adjust=1, kernel="gaussian", trim=FALSE, na.rm=FALSE, ...)

Density estimation, 1D

This page describes stat_density, 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_density. Aesthetics are mapped to variables in the data with the aes function: stat_density(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
y..density..continuous, date, datetime, discrete
fillNAbrewer, gradient, gradient2, gradientn, grey, hue, identity, manual

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.

See also

Examples

> m <- ggplot(movies, aes(x=rating)) 
> m + geom_density() 
  
>  
> # Adjust parameters 
> m + geom_density(kernel = "rectangular") 
  
> m + geom_density(kernel = "biweight") 
  
> m + geom_density(kernel = "epanechnikov") 
  
> m + geom_density(adjust=1/5) # Very rough 
  
> m + geom_density(adjust=5) # Very smooth 
  
>  
> # Adjust aesthetics 
> m + geom_density(aes(fill=factor(Drama)), size=2) 
  
> # Scale so peaks have same height: 
> m + geom_density(aes(fill=factor(Drama), y = ..scaled..), size=2) 
  
>  
> m + geom_density(colour="darkgreen", size=2) 
  
> m + geom_density(colour="darkgreen", size=2, fill=NA) 
  
> m + geom_density(colour="darkgreen", size=2, fill="green") 
  
>  
> # Change scales 
> (m <- ggplot(movies, aes(x=votes)) + geom_density(trim = TRUE)) 
  
> m + scale_x_log10() 
  
> m + coord_trans(x="log10") 
  
> m + scale_x_log10() + coord_trans(x="log10") 
  
>  
> # Also useful with 
> m + stat_bin() 
stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.

  
>  
> # Make a volcano plot 
> ggplot(diamonds, aes(x = price)) + 
+   stat_density(aes(ymax = ..density..,  ymin = -..density..), 
+     fill = "grey50", colour = "grey50", 
+     geom = "ribbon", position = "identity") + 
+   facet_grid(. ~ cut) + 
+   coord_flip() 
  
>  
> # Stacked density plots 
> # If you want to create a stacked density plot, you need to use 
> # the 'count' (density * n) variable instead of the default density 
>  
> # Loses marginal densities 
> qplot(rating, ..density.., data=movies, geom="density", fill=mpaa, 
+ position="stack") 
  
> # Preserves marginal densities 
> qplot(rating, ..count.., data=movies, geom="density", fill=mpaa, 
+ position="stack") 
  
>  
> # You can use position="fill" to produce a conditional density estimate 
> qplot(rating, ..count.., data=movies, geom="density", fill=mpaa, position="fill") 
  
>  
> # Need to be careful with weighted data 
> m <- ggplot(movies, aes(x=rating, weight=votes)) 
> m + geom_histogram(aes(y = ..count..)) + geom_density(fill=NA) 
stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.

Warning: sum(weights) != 1  -- will not get true density
  
>  
> m <- ggplot(movies, aes(x=rating, weight=votes/sum(votes))) 
> m + geom_histogram(aes(y=..density..)) + geom_density(fill=NA, colour="black") 
stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.

  
>  
> movies$decade <- round_any(movies$year, 10) 
> m <- ggplot(movies, aes(x=rating, colour=decade, group=decade)) 
> m + geom_density(fill=NA) 
  
> m + geom_density(fill=NA) + aes(y = ..count..) 
  
>  
> # Use qplot instead 
> qplot(length, data=movies, geom="density", weight=rating) 
Warning: sum(weights) != 1  -- will not get true density
  
> qplot(length, data=movies, geom="density", weight=rating/sum(rating)) 
  

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