EVOLUTION-MANAGER
Edit File: barplot.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 Plots</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 barplot {graphics}"><tr><td>barplot {graphics}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Bar Plots</h2> <h3>Description</h3> <p>Creates a bar plot with vertical or horizontal bars. </p> <h3>Usage</h3> <pre> barplot(height, ...) ## Default S3 method: barplot(height, width = 1, space = NULL, names.arg = NULL, legend.text = NULL, beside = FALSE, horiz = FALSE, density = NULL, angle = 45, col = NULL, border = par("fg"), main = NULL, sub = NULL, xlab = NULL, ylab = NULL, xlim = NULL, ylim = NULL, xpd = TRUE, log = "", axes = TRUE, axisnames = TRUE, cex.axis = par("cex.axis"), cex.names = par("cex.axis"), inside = TRUE, plot = TRUE, axis.lty = 0, offset = 0, add = FALSE, ann = !add && par("ann"), args.legend = NULL, ...) ## S3 method for class 'formula' barplot(formula, data, subset, na.action, horiz = FALSE, xlab = NULL, ylab = NULL, ...) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>height</code></td> <td> <p>either a vector or matrix of values describing the bars which make up the plot. If <code>height</code> is a vector, the plot consists of a sequence of rectangular bars with heights given by the values in the vector. If <code>height</code> is a matrix and <code>beside</code> is <code>FALSE</code> then each bar of the plot corresponds to a column of <code>height</code>, with the values in the column giving the heights of stacked sub-bars making up the bar. If <code>height</code> is a matrix and <code>beside</code> is <code>TRUE</code>, then the values in each column are juxtaposed rather than stacked.</p> </td></tr> <tr valign="top"><td><code>width</code></td> <td> <p>optional vector of bar widths. Re-cycled to length the number of bars drawn. Specifying a single value will have no visible effect unless <code>xlim</code> is specified.</p> </td></tr> <tr valign="top"><td><code>space</code></td> <td> <p>the amount of space (as a fraction of the average bar width) left before each bar. May be given as a single number or one number per bar. If <code>height</code> is a matrix and <code>beside</code> is <code>TRUE</code>, <code>space</code> may be specified by two numbers, where the first is the space between bars in the same group, and the second the space between the groups. If not given explicitly, it defaults to <code>c(0,1)</code> if <code>height</code> is a matrix and <code>beside</code> is <code>TRUE</code>, and to 0.2 otherwise.</p> </td></tr> <tr valign="top"><td><code>names.arg</code></td> <td> <p>a vector of names to be plotted below each bar or group of bars. If this argument is omitted, then the names are taken from the <code>names</code> attribute of <code>height</code> if this is a vector, or the column names if it is a matrix.</p> </td></tr> <tr valign="top"><td><code>legend.text</code></td> <td> <p>a vector of text used to construct a legend for the plot, or a logical indicating whether a legend should be included. This is only useful when <code>height</code> is a matrix. In that case given legend labels should correspond to the rows of <code>height</code>; if <code>legend.text</code> is true, the row names of <code>height</code> will be used as labels if they are non-null.</p> </td></tr> <tr valign="top"><td><code>beside</code></td> <td> <p>a logical value. If <code>FALSE</code>, the columns of <code>height</code> are portrayed as stacked bars, and if <code>TRUE</code> the columns are portrayed as juxtaposed bars.</p> </td></tr> <tr valign="top"><td><code>horiz</code></td> <td> <p>a logical value. If <code>FALSE</code>, the bars are drawn vertically with the first bar to the left. If <code>TRUE</code>, the bars are drawn horizontally with the first at the bottom.</p> </td></tr> <tr valign="top"><td><code>density</code></td> <td> <p>a vector giving the density of shading lines, in lines per inch, for the bars or bar components. The default value of <code>NULL</code> means that no shading lines are drawn. Non-positive values of <code>density</code> also inhibit the drawing of shading lines.</p> </td></tr> <tr valign="top"><td><code>angle</code></td> <td> <p>the slope of shading lines, given as an angle in degrees (counter-clockwise), for the bars or bar components.</p> </td></tr> <tr valign="top"><td><code>col</code></td> <td> <p>a vector of colors for the bars or bar components. By default, grey is used if <code>height</code> is a vector, and a gamma-corrected grey palette if <code>height</code> is a matrix.</p> </td></tr> <tr valign="top"><td><code>border</code></td> <td> <p>the color to be used for the border of the bars. Use <code>border = NA</code> to omit borders. If there are shading lines, <code>border = TRUE</code> means use the same colour for the border as for the shading lines.</p> </td></tr> <tr valign="top"><td><code>main,sub</code></td> <td> <p>overall and sub title for the plot.</p> </td></tr> <tr valign="top"><td><code>xlab</code></td> <td> <p>a label for the x axis.</p> </td></tr> <tr valign="top"><td><code>ylab</code></td> <td> <p>a label for the y axis.</p> </td></tr> <tr valign="top"><td><code>xlim</code></td> <td> <p>limits for the x axis.</p> </td></tr> <tr valign="top"><td><code>ylim</code></td> <td> <p>limits for the y axis.</p> </td></tr> <tr valign="top"><td><code>xpd</code></td> <td> <p>logical. Should bars be allowed to go outside region?</p> </td></tr> <tr valign="top"><td><code>log</code></td> <td> <p>string specifying if axis scales should be logarithmic; see <code><a href="plot.default.html">plot.default</a></code>.</p> </td></tr> <tr valign="top"><td><code>axes</code></td> <td> <p>logical. If <code>TRUE</code>, a vertical (or horizontal, if <code>horiz</code> is true) axis is drawn.</p> </td></tr> <tr valign="top"><td><code>axisnames</code></td> <td> <p>logical. If <code>TRUE</code>, and if there are <code>names.arg</code> (see above), the other axis is drawn (with <code>lty = 0</code>) and labeled.</p> </td></tr> <tr valign="top"><td><code>cex.axis</code></td> <td> <p>expansion factor for numeric axis labels.</p> </td></tr> <tr valign="top"><td><code>cex.names</code></td> <td> <p>expansion factor for axis names (bar labels).</p> </td></tr> <tr valign="top"><td><code>inside</code></td> <td> <p>logical. If <code>TRUE</code>, the lines which divide adjacent (non-stacked!) bars will be drawn. Only applies when <code>space = 0</code> (which it partly is when <code>beside = TRUE</code>).</p> </td></tr> </table> <table summary="R argblock"> <tr valign="top"><td><code>plot</code></td> <td> <p>logical. If <code>FALSE</code>, nothing is plotted.</p> </td></tr> <tr valign="top"><td><code>axis.lty</code></td> <td> <p>the graphics parameter <code>lty</code> applied to the axis and tick marks of the categorical (default horizontal) axis. Note that by default the axis is suppressed.</p> </td></tr> <tr valign="top"><td><code>offset</code></td> <td> <p>a vector indicating how much the bars should be shifted relative to the x axis.</p> </td></tr> <tr valign="top"><td><code>add</code></td> <td> <p>logical specifying if bars should be added to an already existing plot; defaults to <code>FALSE</code>.</p> </td></tr> <tr valign="top"><td><code>ann</code></td> <td> <p>logical specifying if the default annotation (<code>main</code>, <code>sub</code>, <code>xlab</code>, <code>ylab</code>) should appear on the plot, see <code><a href="title.html">title</a></code>.</p> </td></tr> <tr valign="top"><td><code>args.legend</code></td> <td> <p>list of additional arguments to pass to <code><a href="legend.html">legend</a>()</code>; names of the list are used as argument names. Only used if <code>legend.text</code> is supplied.</p> </td></tr> <tr valign="top"><td><code>formula</code></td> <td> <p>a formula where the <code>y</code> variables are numeric data to plot against the categorical <code>x</code> variables. The formula can have one of three forms: </p> <pre> y ~ x y ~ x1 + x2 cbind(y1, y2) ~ x</pre><p>, see the examples.</p> </td></tr> <tr valign="top"><td><code>data</code></td> <td> <p>a data frame (or list) from which the variables in formula should be taken.</p> </td></tr> <tr valign="top"><td><code>subset</code></td> <td> <p>an optional vector specifying a subset of observations to be used.</p> </td></tr> <tr valign="top"><td><code>na.action</code></td> <td> <p>a function which indicates what should happen when the data contain <code><a href="../../base/html/NA.html">NA</a></code> values. The default is to ignore missing values in the given variables.</p> </td></tr> <tr valign="top"><td><code>...</code></td> <td> <p>arguments to be passed to/from other methods. For the default method these can include further arguments (such as <code>axes</code>, <code>asp</code> and <code>main</code>) and <a href="par.html">graphical parameters</a> (see <code><a href="par.html">par</a></code>) which are passed to <code><a href="plot.window.html">plot.window</a>()</code>, <code><a href="title.html">title</a>()</code> and <code><a href="axis.html">axis</a></code>.</p> </td></tr> </table> <h3>Value</h3> <p>A numeric vector (or matrix, when <code>beside = TRUE</code>), say <code>mp</code>, giving the coordinates of <em>all</em> the bar midpoints drawn, useful for adding to the graph. </p> <p>If <code>beside</code> is true, use <code>colMeans(mp)</code> for the midpoints of each <em>group</em> of bars, see example. </p> <h3>Author(s)</h3> <p>R Core, with a contribution by Arni Magnusson.</p> <h3>References</h3> <p>Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) <em>The New S Language</em>. Wadsworth & Brooks/Cole. </p> <p>Murrell, P. (2005) <em>R Graphics</em>. Chapman & Hall/CRC Press. </p> <h3>See Also</h3> <p><code><a href="plot.html">plot</a>(..., type = "h")</code>, <code><a href="dotchart.html">dotchart</a></code>; <code><a href="hist.html">hist</a></code> for bars of a <em>continuous</em> variable. <code><a href="mosaicplot.html">mosaicplot</a>()</code>, more sophisticated to visualize <em>several</em> categorical variables. </p> <h3>Examples</h3> <pre> # Formula method barplot(GNP ~ Year, data = longley) barplot(cbind(Employed, Unemployed) ~ Year, data = longley) ## 3rd form of formula - 2 categories : op <- par(mfrow = 2:1, mgp = c(3,1,0)/2, mar = .1+c(3,3:1)) summary(d.Titanic <- as.data.frame(Titanic)) barplot(Freq ~ Class + Survived, data = d.Titanic, subset = Age == "Adult" & Sex == "Male", main = "barplot(Freq ~ Class + Survived, *)", ylab = "# {passengers}", legend = TRUE) # Corresponding table : (xt <- xtabs(Freq ~ Survived + Class + Sex, d.Titanic, subset = Age=="Adult")) # Alternatively, a mosaic plot : mosaicplot(xt[,,"Male"], main = "mosaicplot(Freq ~ Class + Survived, *)", color=TRUE) par(op) # Default method require(grDevices) # for colours tN <- table(Ni <- stats::rpois(100, lambda = 5)) r <- barplot(tN, col = rainbow(20)) #- type = "h" plotting *is* 'bar'plot lines(r, tN, type = "h", col = "red", lwd = 2) barplot(tN, space = 1.5, axisnames = FALSE, sub = "barplot(..., space= 1.5, axisnames = FALSE)") barplot(VADeaths, plot = FALSE) barplot(VADeaths, plot = FALSE, beside = TRUE) mp <- barplot(VADeaths) # default tot <- colMeans(VADeaths) text(mp, tot + 3, format(tot), xpd = TRUE, col = "blue") barplot(VADeaths, beside = TRUE, col = c("lightblue", "mistyrose", "lightcyan", "lavender", "cornsilk"), legend = rownames(VADeaths), ylim = c(0, 100)) title(main = "Death Rates in Virginia", font.main = 4) hh <- t(VADeaths)[, 5:1] mybarcol <- "gray20" mp <- barplot(hh, beside = TRUE, col = c("lightblue", "mistyrose", "lightcyan", "lavender"), legend = colnames(VADeaths), ylim = c(0,100), main = "Death Rates in Virginia", font.main = 4, sub = "Faked upper 2*sigma error bars", col.sub = mybarcol, cex.names = 1.5) segments(mp, hh, mp, hh + 2*sqrt(1000*hh/100), col = mybarcol, lwd = 1.5) stopifnot(dim(mp) == dim(hh)) # corresponding matrices mtext(side = 1, at = colMeans(mp), line = -2, text = paste("Mean", formatC(colMeans(hh))), col = "red") # Bar shading example barplot(VADeaths, angle = 15+10*1:5, density = 20, col = "black", legend = rownames(VADeaths)) title(main = list("Death Rates in Virginia", font = 4)) # Border color barplot(VADeaths, border = "dark blue") # Log scales (not much sense here) barplot(tN, col = heat.colors(12), log = "y") barplot(tN, col = gray.colors(20), log = "xy") # Legend location barplot(height = cbind(x = c(465, 91) / 465 * 100, y = c(840, 200) / 840 * 100, z = c(37, 17) / 37 * 100), beside = FALSE, width = c(465, 840, 37), col = c(1, 2), legend.text = c("A", "B"), args.legend = list(x = "topleft")) </pre> <hr /><div style="text-align: center;">[Package <em>graphics</em> version 3.6.0 <a href="00Index.html">Index</a>]</div> </body></html>