stat_bin

stat_bin(mapping=NULL, data=NULL, geom="bar", position="stack", width=0.9, drop=FALSE, ...)

Bin data

Missing values are currently silently dropped.

This page describes stat_bin, 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_bin. Aesthetics are mapped to variables in the data with the aes function: stat_bin(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..count..continuous, date, datetime, discrete

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)) 
> m + stat_bin() 
stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.

  
> m + stat_bin(binwidth=0.1) 
  
> m + stat_bin(breaks=seq(4,6, by=0.1)) 
  
> # See geom_histogram for more histogram examples 
>  
> # To create a unit area histogram, use aes(y = ..density..) 
> (linehist <- m + stat_bin(aes(y = ..density..), binwidth=0.1, 
+   geom="line", position="identity")) 
  
> linehist + stat_density(colour="blue", fill=NA) 
  
>  
> # Also works with categorical variables 
> ggplot(movies, aes(x=mpaa)) + stat_bin() 
  
> qplot(mpaa, data=movies, stat="bin") 
  

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