EVOLUTION-MANAGER
Edit File: geom_bar.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: Bar charts</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_bar {ggplot2}"><tr><td>geom_bar {ggplot2}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Bar charts</h2> <h3>Description</h3> <p>There are two types of bar charts: <code>geom_bar()</code> and <code>geom_col()</code>. <code>geom_bar()</code> makes the height of the bar proportional to the number of cases in each group (or if the <code>weight</code> aesthetic is supplied, the sum of the weights). If you want the heights of the bars to represent values in the data, use <code>geom_col()</code> instead. <code>geom_bar()</code> uses <code>stat_count()</code> by default: it counts the number of cases at each x position. <code>geom_col()</code> uses <code>stat_identity()</code>: it leaves the data as is. </p> <h3>Usage</h3> <pre> geom_bar( mapping = NULL, data = NULL, stat = "count", position = "stack", ..., width = NULL, na.rm = FALSE, orientation = NA, show.legend = NA, inherit.aes = TRUE ) geom_col( mapping = NULL, data = NULL, position = "stack", ..., width = NULL, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE ) stat_count( mapping = NULL, data = NULL, geom = "bar", position = "stack", ..., width = NULL, 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>width</code></td> <td> <p>Bar width. By default, set to 90% of the resolution of the data.</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>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>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>geom, stat</code></td> <td> <p>Override the default connection between <code>geom_bar()</code> and <code>stat_count()</code>.</p> </td></tr> </table> <h3>Details</h3> <p>A bar chart uses height to represent a value, and so the base of the bar must always be shown to produce a valid visual comparison. Proceed with caution when using transformed scales with a bar chart. It's important to always use a meaningful reference point for the base of the bar. For example, for log transformations the reference point is 1. In fact, when using a log scale, <code>geom_bar()</code> automatically places the base of the bar at 1. Furthermore, never use stacked bars with a transformed scale, because scaling happens before stacking. As a consequence, the height of bars will be wrong when stacking occurs with a transformed scale. </p> <p>By default, multiple bars occupying the same <code>x</code> position will be stacked atop one another by <code><a href="position_stack.html">position_stack()</a></code>. If you want them to be dodged side-to-side, use <code><a href="position_dodge.html">position_dodge()</a></code> or <code><a href="position_dodge.html">position_dodge2()</a></code>. Finally, <code><a href="position_stack.html">position_fill()</a></code> shows relative proportions at each <code>x</code> by stacking the bars and then standardising each bar to have the same height. </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_bar()</code> understands the following aesthetics (required aesthetics are in bold): </p> <ul> <li> <p><strong><code>x</code></strong> </p> </li> <li> <p><strong><code>y</code></strong> </p> </li> <li> <p><code>alpha</code> </p> </li> <li> <p><code>colour</code> </p> </li> <li> <p><code>fill</code> </p> </li> <li> <p><code>group</code> </p> </li> <li> <p><code>linetype</code> </p> </li> <li> <p><code>size</code> </p> </li></ul> <p>Learn more about setting these aesthetics in <code>vignette("ggplot2-specs")</code>. </p> <p><code>geom_col()</code> understands the following aesthetics (required aesthetics are in bold): </p> <ul> <li> <p><strong><code>x</code></strong> </p> </li> <li> <p><strong><code>y</code></strong> </p> </li> <li> <p><code>alpha</code> </p> </li> <li> <p><code>colour</code> </p> </li> <li> <p><code>fill</code> </p> </li> <li> <p><code>group</code> </p> </li> <li> <p><code>linetype</code> </p> </li> <li> <p><code>size</code> </p> </li></ul> <p>Learn more about setting these aesthetics in <code>vignette("ggplot2-specs")</code>. </p> <p><code>stat_count()</code> understands the following aesthetics (required aesthetics are in bold): </p> <ul> <li> <p><strong><code>x</code> <em>or</em> <code>y</code></strong> </p> </li> <li> <p><code>group</code> </p> </li> <li> <p><code>weight</code> </p> </li></ul> <p>Learn more about setting these aesthetics in <code>vignette("ggplot2-specs")</code>. </p> <h3>Computed variables</h3> <dl> <dt>count</dt><dd><p>number of points in bin</p> </dd> <dt>prop</dt><dd><p>groupwise proportion</p> </dd> </dl> <h3>See Also</h3> <p><code><a href="geom_histogram.html">geom_histogram()</a></code> for continuous data, <code><a href="position_dodge.html">position_dodge()</a></code> and <code><a href="position_dodge.html">position_dodge2()</a></code> for creating side-by-side bar charts. </p> <p><code><a href="geom_histogram.html">stat_bin()</a></code>, which bins data in ranges and counts the cases in each range. It differs from <code>stat_count</code>, which counts the number of cases at each <code>x</code> position (without binning into ranges). <code><a href="geom_histogram.html">stat_bin()</a></code> requires continuous <code>x</code> data, whereas <code>stat_count</code> can be used for both discrete and continuous <code>x</code> data. </p> <h3>Examples</h3> <pre> # geom_bar is designed to make it easy to create bar charts that show # counts (or sums of weights) g <- ggplot(mpg, aes(class)) # Number of cars in each class: g + geom_bar() # Total engine displacement of each class g + geom_bar(aes(weight = displ)) # Map class to y instead to flip the orientation ggplot(mpg) + geom_bar(aes(y = class)) # Bar charts are automatically stacked when multiple bars are placed # at the same location. The order of the fill is designed to match # the legend g + geom_bar(aes(fill = drv)) # If you need to flip the order (because you've flipped the orientation) # call position_stack() explicitly: ggplot(mpg, aes(y = class)) + geom_bar(aes(fill = drv), position = position_stack(reverse = TRUE)) + theme(legend.position = "top") # To show (e.g.) means, you need geom_col() df <- data.frame(trt = c("a", "b", "c"), outcome = c(2.3, 1.9, 3.2)) ggplot(df, aes(trt, outcome)) + geom_col() # But geom_point() displays exactly the same information and doesn't # require the y-axis to touch zero. ggplot(df, aes(trt, outcome)) + geom_point() # You can also use geom_bar() with continuous data, in which case # it will show counts at unique locations df <- data.frame(x = rep(c(2.9, 3.1, 4.5), c(5, 10, 4))) ggplot(df, aes(x)) + geom_bar() # cf. a histogram of the same data ggplot(df, aes(x)) + geom_histogram(binwidth = 0.5) </pre> <hr /><div style="text-align: center;">[Package <em>ggplot2</em> version 3.3.2 <a href="00Index.html">Index</a>]</div> </body></html>