gbarplot | R Documentation |
Doug's alternative version of the barplot
function. Numeric x
values may be passed directly for proper positioning along the
X axis, allowing for easy overlay in existing plots. If both positive and negative 'y'
values are present, then the resulting stacked plot has bars separated above and below the X
axis.
gbarplot(
y,
x,
labels,
width = 1,
col = "grey70",
border = "grey50",
add = FALSE,
grid = FALSE,
yaxs = ifelse(all(y[!is.na(y)] <= 0) | all(y[!is.na(y)] >= 0), "i", "r"),
legend = TRUE,
...
)
y |
A numeric vector, table, matrix or data frame object containing numeric values which are to be displayed as a bar plot. Each row of the matrix will be plotted as a separate bar. |
x |
A vector specifying the numeric values or labels to be used for separating each bar along the horizontal axis. |
labels |
A character vector specifying labels to be used for labelling each bar along the horizontal
axis. The number of labels must equal or be consistent with those of |
width |
A numeric value from 0 to 1 specifying the relative width of each bar. A value of zero corresponds
to a bar with no width while a value of one implies that adjacent bars are touching (i.e. there
is no space between the bars). The default value is |
col |
Vector specifying the colour(s) of the bars to be drawn. Each column of 'y' is drawn in a separate
colour. The default colour for a single variable is "grey". If there is more than one variable
and |
border |
Border colour(s) of the bars. |
add |
A logical value specifying if bars should be added to an existing plot. The default is |
grid |
Logical value specifying whether to draw a grid. |
yaxs |
A character value specifying how to set the limits of the Y axis, see |
legend |
A logical value specifying whether a legend is to be drawn. The default is |
... |
Further arguments to be passed onto the |
error.bar
# Generate random vector:
y <- 10*rnorm(5)+5
# Simple barplot:
gbarplot(y)
# Same barplot with explicit 'x':
gbarplot(y, x = c(1, 2, 4, 5, 7))
# Add explicit labels:
gbarplot(y, x = c(1, 2, 4, 5, 7), labels = letters[1:5])
# Display bar plot:
y <- data.frame("Variable 1" = c(1, -2, NA, -4, 5), "Variable 2" = 1:5)
gbarplot(y, xlab = "Order", ylab = "y",
labels = c("First", "Second", "Third", "Fourth", "Fifth"))
# Display a frequency table overlayed by the true distribution:
y <- table(round(rnorm(250, sd = 5)))
gbarplot(y, col = "grey", width = 1)
x <- seq(-15, 15, len = 100)
lines(x, 250 * dnorm(x, sd = 5), col = "red", lwd = 2)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.