geom_freqpoly(mapping = NULL, data = NULL, stat = "bin", position = "identity", show.legend = NA, inherit.aes = TRUE, ...)geom_histogram(mapping = NULL, data = NULL, stat = "bin", binwidth = NULL, bins = NULL, origin = NULL, right = FALSE, position = "stack", show.legend = NA, inherit.aes = TRUE, ...)stat_bin(mapping = NULL, data = NULL, geom = "bar", position = "stack", width = 0.9, drop = FALSE, right = FALSE, binwidth = NULL, bins = NULL, origin = NULL, breaks = NULL, show.legend = NA, inherit.aes = TRUE, ...)
aes
or
aes_
. If specified and inherit.aes = TRUE
(the
default), is combined with the default mapping at the top level of the
plot. You only need to supply mapping
if there isn't a mapping
defined for the plot.NA
, the default, includes if any aesthetics are mapped.
FALSE
never includes, and TRUE
always includes.FALSE
, overrides the default aesthetics,
rather than combining with them. This is most useful for helper functions
that define both data and aesthetics and shouldn't inherit behaviour from
the default plot specification, e.g. borders
.layer
. There are
three types of arguments you can use here:
color = "red"
or size = 3
.
stat
associated with the layer.
bins
of the range of
the databinwidth
or breaks
.
Defaults to 30TRUE
, right-closed, left-open, if FALSE
,
the default, right-open, left-closed.geom_histogram
/geom_freqpoly
and stat_bin
.Display a 1d distribution by dividing into bins and counting the number of observations in each bin. Histograms use bars; frequency polygons use lines.
stat_bin
is suitable only for continuous x data. If your x data is
discrete, you probably want to use stat_count
.
By default, stat_bin
uses 30 bins - this is not a good default,
but the idea is to get you experimenting with different binwidths. You
may need to look at a few to uncover the full story behind your data.
geom_histogram
uses the same aesthetics as geom_bar
;
geom_freqpoly
uses the same aesthetics as geom_line
.
ggplot(diamonds, aes(carat)) + geom_histogram()`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
ggplot(diamonds, aes(carat)) + geom_histogram(binwidth = 0.01)
ggplot(diamonds, aes(carat)) + geom_histogram(bins = 200)
# Rather than stacking histograms, it's easier to compare frequency # polygons ggplot(diamonds, aes(price, fill = cut)) + geom_histogram(binwidth = 500)
ggplot(diamonds, aes(price, colour = cut)) + geom_freqpoly(binwidth = 500)
# To make it easier to compare distributions with very different counts, # put density on the y axis instead of the default count ggplot(diamonds, aes(price, ..density.., colour = cut)) + geom_freqpoly(binwidth = 500)
if (require("ggplot2movies")) { # Often we don't want the height of the bar to represent the # count of observations, but the sum of some other variable. # For example, the following plot shows the number of movies # in each rating. m <- ggplot(movies, aes(rating)) m + geom_histogram(binwidth = 0.1) # If, however, we want to see the number of votes cast in each # category, we need to weight by the votes variable m + geom_histogram(aes(weight = votes), binwidth = 0.1) + ylab("votes") # For transformed scales, binwidth applies to the transformed data. # The bins have constant width on the transformed scale. m + geom_histogram() + scale_x_log10() m + geom_histogram(binwidth = 0.05) + scale_x_log10() # For transformed coordinate systems, the binwidth applies to the # raw data. The bins have constant width on the original scale. # Using log scales does not work here, because the first # bar is anchored at zero, and so when transformed becomes negative # infinity. This is not a problem when transforming the scales, because # no observations have 0 ratings. m + geom_histogram(origin = 0) + coord_trans(x = "log10") # Use origin = 0, to make sure we don't take sqrt of negative values m + geom_histogram(origin = 0) + coord_trans(x = "sqrt") # You can also transform the y axis. Remember that the base of the bars # has value 0, so log transformations are not appropriate m <- ggplot(movies, aes(x = rating)) m + geom_histogram(binwidth = 0.5) + scale_y_sqrt() }Loading required package: ggplot2movies Warning message: there is no package called ‘ggplot2movies’rm(movies)Warning message: object 'movies' not found
stat_count
, which counts the number of cases at each x
posotion, without binning. It is suitable for both discrete and continuous
x data, whereas stat_bin is suitable only for continuous x data.