stat_bin

0th

Percentile

Bin data.

Missing values are currently silently dropped.

Usage
stat_bin(mapping = NULL, data = NULL, geom = "bar",
    position = "stack", width = 0.9, drop = FALSE,
    right = FALSE, binwidth = NULL, origin = NULL,
    breaks = NULL, ...)
Arguments
binwidth
Bin width to use. Defaults to 1/30 of the range of the data
breaks
Actual breaks to use. Overrides bin width and origin
origin
Origin of first bin
width
Width of bars when used with categorical data
right
If TRUE, right-closed, left-open, if FALSE,
drop
If TRUE, remove all bins with zero counts
mapping
The aesthetic mapping, usually constructed with aes or aes_string. Only needs to be set at the layer level if you are overriding the plot defaults.
data
A layer specific dataset - only needed if you want to override the plot defaults.
geom
The geometric object to use display the data
position
The position adjustment to use for overlappling points on this layer
...
other arguments passed on to layer. This can include aesthetics whose values you want to set, not map. See layer for more details.
Value

  • New data frame with additional columns:
  • countnumber of points in bin
  • densitydensity of points in bin, scaled to integrate to 1
  • ncountcount, scaled to maximum of 1
  • ndensitydensity, scaled to maximum of 1

Aesthetics

[results=rd,stage=build]{ggplot2:::rd_aesthetics("stat", "bin")} simple <- data.frame(x = rep(1:10, each = 2)) base <- ggplot(simple, aes(x)) # By default, right = TRUE, and intervals are of the form (a, b] base + stat_bin(binwidth = 1, drop = FALSE, right = TRUE, col = "black") # If right = FALSE intervals are of the form [a, b) base + stat_bin(binwidth = 1, drop = FALSE, right = FALSE, col = "black")

m <- ggplot(movies, aes(x=rating)) m + stat_bin() 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")

Aliases
  • stat_bin
Documentation reproduced from package ggplot2, version 0.9.3.1, License: GPL-2

Community examples

joshua.barillas@aacsb.edu at Apr 19, 2018 ggplot2 v1.0.1

## Example ```r simple <- data.frame(x = rep(1:10, each = 2)) base <- ggplot(simple, aes(x)) # By default, right = FALSE intervals are of the form [a, b) base + stat_bin(binwidth = 1, drop = FALSE, right = FALSE, col = "black") # If right = TRUE, and intervals are of the form (a, b] base + stat_bin(binwidth = 1, drop = FALSE, right = TRUE, col = "black") m <- ggplot(movies, aes(x = rating)) m + stat_bin() 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") ```