gbarplot: Gulf Bar Plot

View source: R/gbarplot.R

gbarplotR Documentation

Gulf Bar Plot

Description

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.

Usage

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,
  ...
)

Arguments

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 x or y.

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 0.8.

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 col is unspecified, then colours are interpolated from a grayscale palette.

border

Border colour(s) of the bars.

add

A logical value specifying if bars should be added to an existing plot. The default is FALSE.

grid

Logical value specifying whether to draw a grid.

yaxs

A character value specifying how to set the limits of the Y axis, see par.

legend

A logical value specifying whether a legend is to be drawn. The default is TRUE. The legend entries are taken from the columns names of y.

...

Further arguments to be passed onto the plot function.

See Also

error.bar

Examples


# 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)


TobieSurette/gulf.graphics documentation built on Feb. 10, 2024, 10:58 p.m.