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")
Run the code above in your browser using DataCamp Workspace