EVOLUTION-MANAGER
Edit File: geom_histogram.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Histograms and frequency polygons</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for geom_freqpoly {ggplot2}"><tr><td>geom_freqpoly {ggplot2}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Histograms and frequency polygons</h2> <h3>Description</h3> <p>Visualise the distribution of a single continuous variable by dividing the x axis into bins and counting the number of observations in each bin. Histograms (<code>geom_histogram()</code>) display the counts with bars; frequency polygons (<code>geom_freqpoly()</code>) display the counts with lines. Frequency polygons are more suitable when you want to compare the distribution across the levels of a categorical variable. </p> <h3>Usage</h3> <pre> geom_freqpoly( mapping = NULL, data = NULL, stat = "bin", position = "identity", ..., na.rm = FALSE, show.legend = NA, inherit.aes = TRUE ) geom_histogram( mapping = NULL, data = NULL, stat = "bin", position = "stack", ..., binwidth = NULL, bins = NULL, na.rm = FALSE, orientation = NA, show.legend = NA, inherit.aes = TRUE ) stat_bin( mapping = NULL, data = NULL, geom = "bar", position = "stack", ..., binwidth = NULL, bins = NULL, center = NULL, boundary = NULL, breaks = NULL, closed = c("right", "left"), pad = FALSE, na.rm = FALSE, orientation = NA, show.legend = NA, inherit.aes = TRUE ) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>mapping</code></td> <td> <p>Set of aesthetic mappings created by <code><a href="aes.html">aes()</a></code> or <code><a href="aes_.html">aes_()</a></code>. If specified and <code>inherit.aes = TRUE</code> (the default), it is combined with the default mapping at the top level of the plot. You must supply <code>mapping</code> if there is no plot mapping.</p> </td></tr> <tr valign="top"><td><code>data</code></td> <td> <p>The data to be displayed in this layer. There are three options: </p> <p>If <code>NULL</code>, the default, the data is inherited from the plot data as specified in the call to <code><a href="ggplot.html">ggplot()</a></code>. </p> <p>A <code>data.frame</code>, or other object, will override the plot data. All objects will be fortified to produce a data frame. See <code><a href="fortify.html">fortify()</a></code> for which variables will be created. </p> <p>A <code>function</code> will be called with a single argument, the plot data. The return value must be a <code>data.frame</code>, and will be used as the layer data. A <code>function</code> can be created from a <code>formula</code> (e.g. <code>~ head(.x, 10)</code>).</p> </td></tr> <tr valign="top"><td><code>position</code></td> <td> <p>Position adjustment, either as a string, or the result of a call to a position adjustment function.</p> </td></tr> <tr valign="top"><td><code>...</code></td> <td> <p>Other arguments passed on to <code><a href="layer.html">layer()</a></code>. These are often aesthetics, used to set an aesthetic to a fixed value, like <code>colour = "red"</code> or <code>size = 3</code>. They may also be parameters to the paired geom/stat.</p> </td></tr> <tr valign="top"><td><code>na.rm</code></td> <td> <p>If <code>FALSE</code>, the default, missing values are removed with a warning. If <code>TRUE</code>, missing values are silently removed.</p> </td></tr> <tr valign="top"><td><code>show.legend</code></td> <td> <p>logical. Should this layer be included in the legends? <code>NA</code>, the default, includes if any aesthetics are mapped. <code>FALSE</code> never includes, and <code>TRUE</code> always includes. It can also be a named logical vector to finely select the aesthetics to display.</p> </td></tr> <tr valign="top"><td><code>inherit.aes</code></td> <td> <p>If <code>FALSE</code>, 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. <code><a href="borders.html">borders()</a></code>.</p> </td></tr> <tr valign="top"><td><code>binwidth</code></td> <td> <p>The width of the bins. Can be specified as a numeric value or as a function that calculates width from unscaled x. Here, "unscaled x" refers to the original x values in the data, before application of any scale transformation. When specifying a function along with a grouping structure, the function will be called once per group. The default is to use the number of bins in <code>bins</code>, covering the range of the data. You should always override this value, exploring multiple widths to find the best to illustrate the stories in your data. </p> <p>The bin width of a date variable is the number of days in each time; the bin width of a time variable is the number of seconds.</p> </td></tr> <tr valign="top"><td><code>bins</code></td> <td> <p>Number of bins. Overridden by <code>binwidth</code>. Defaults to 30.</p> </td></tr> <tr valign="top"><td><code>orientation</code></td> <td> <p>The orientation of the layer. The default (<code>NA</code>) automatically determines the orientation from the aesthetic mapping. In the rare event that this fails it can be given explicitly by setting <code>orientation</code> to either <code>"x"</code> or <code>"y"</code>. See the <em>Orientation</em> section for more detail.</p> </td></tr> <tr valign="top"><td><code>geom, stat</code></td> <td> <p>Use to override the default connection between <code>geom_histogram()</code>/<code>geom_freqpoly()</code> and <code>stat_bin()</code>.</p> </td></tr> <tr valign="top"><td><code>center, boundary</code></td> <td> <p>bin position specifiers. Only one, <code>center</code> or <code>boundary</code>, may be specified for a single plot. <code>center</code> specifies the center of one of the bins. <code>boundary</code> specifies the boundary between two bins. Note that if either is above or below the range of the data, things will be shifted by the appropriate integer multiple of <code>binwidth</code>. For example, to center on integers use <code>binwidth = 1</code> and <code>center = 0</code>, even if <code>0</code> is outside the range of the data. Alternatively, this same alignment can be specified with <code>binwidth = 1</code> and <code>boundary = 0.5</code>, even if <code>0.5</code> is outside the range of the data.</p> </td></tr> <tr valign="top"><td><code>breaks</code></td> <td> <p>Alternatively, you can supply a numeric vector giving the bin boundaries. Overrides <code>binwidth</code>, <code>bins</code>, <code>center</code>, and <code>boundary</code>.</p> </td></tr> <tr valign="top"><td><code>closed</code></td> <td> <p>One of <code>"right"</code> or <code>"left"</code> indicating whether right or left edges of bins are included in the bin.</p> </td></tr> <tr valign="top"><td><code>pad</code></td> <td> <p>If <code>TRUE</code>, adds empty bins at either end of x. This ensures frequency polygons touch 0. Defaults to <code>FALSE</code>.</p> </td></tr> </table> <h3>Details</h3> <p><code>stat_bin()</code> is suitable only for continuous x data. If your x data is discrete, you probably want to use <code><a href="geom_bar.html">stat_count()</a></code>. </p> <p>By default, the underlying computation (<code>stat_bin()</code>) uses 30 bins; this is not a good default, but the idea is to get you experimenting with different number of bins. You can also experiment modifying the <code>binwidth</code> with <code>center</code> or <code>boundary</code> arguments. <code>binwidth</code> overrides <code>bins</code> so you should do one change at a time. You may need to look at a few options to uncover the full story behind your data. </p> <p>In addition to <code>geom_histogram</code>, you can create a histogram plot by using <code>scale_x_binned()</code> with <code><a href="geom_bar.html">geom_bar()</a></code>. This method by default plots tick marks in between each bar. </p> <h3>Orientation</h3> <p>This geom treats each axis differently and, thus, can thus have two orientations. Often the orientation is easy to deduce from a combination of the given mappings and the types of positional scales in use. Thus, ggplot2 will by default try to guess which orientation the layer should have. Under rare circumstances, the orientation is ambiguous and guessing may fail. In that case the orientation can be specified directly using the <code>orientation</code> parameter, which can be either <code>"x"</code> or <code>"y"</code>. The value gives the axis that the geom should run along, <code>"x"</code> being the default orientation you would expect for the geom. </p> <h3>Aesthetics</h3> <p><code>geom_histogram()</code> uses the same aesthetics as <code><a href="geom_bar.html">geom_bar()</a></code>; <code>geom_freqpoly()</code> uses the same aesthetics as <code><a href="geom_path.html">geom_line()</a></code>. </p> <h3>Computed variables</h3> <dl> <dt>count</dt><dd><p>number of points in bin</p> </dd> <dt>density</dt><dd><p>density of points in bin, scaled to integrate to 1</p> </dd> <dt>ncount</dt><dd><p>count, scaled to maximum of 1</p> </dd> <dt>ndensity</dt><dd><p>density, scaled to maximum of 1</p> </dd> </dl> <h3>See Also</h3> <p><code><a href="geom_bar.html">stat_count()</a></code>, which counts the number of cases at each x position, without binning. It is suitable for both discrete and continuous x data, whereas <code>stat_bin()</code> is suitable only for continuous x data. </p> <h3>Examples</h3> <pre> ggplot(diamonds, aes(carat)) + geom_histogram() ggplot(diamonds, aes(carat)) + geom_histogram(binwidth = 0.01) ggplot(diamonds, aes(carat)) + geom_histogram(bins = 200) # Map values to y to flip the orientation ggplot(diamonds, aes(y = carat)) + geom_histogram() # For histograms with tick marks between each bin, use `geom_bar` with # `scale_x_binned`. ggplot(diamonds, aes(carat)) + geom_bar() + scale_x_binned() # 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, after_stat(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(boundary = 0) + coord_trans(x = "log10") # Use boundary = 0, to make sure we don't take sqrt of negative values m + geom_histogram(boundary = 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() } # You can specify a function for calculating binwidth, which is # particularly useful when faceting along variables with # different ranges because the function will be called once per facet ggplot(economics_long, aes(value)) + facet_wrap(~variable, scales = 'free_x') + geom_histogram(binwidth = function(x) 2 * IQR(x) / (length(x)^(1/3))) </pre> <hr /><div style="text-align: center;">[Package <em>ggplot2</em> version 3.3.2 <a href="00Index.html">Index</a>]</div> </body></html>