Function to add lines and error bars to an existing plot.

draw_lines(
  x,
  y = NULL,
  lb = NULL,
  ub = NULL,
  columns = NULL,
  pch = 19,
  cex = 1.25,
  lwd = 2,
  lty = 1,
  arrow = FALSE,
  length = 0.05,
  col = "black",
  col.p = "black",
  col.eb = col_to_hex("grey", 0.5),
  bg = "white",
  border = NA,
  aes = NULL
)

Arguments

x

Either a numeric vector or a data frame.

y

An optional numeric vector matching in length to x.

lb

An optional numeric vector matching in length to x, specifying the lower bounds for error bars.

ub

An optional numeric vector matching in length to x, specifying the upper bounds for error bars.

columns

A character vector of either 2 or 4 elements, the column names for the x and y-axis values and (optionally) the column names for the lower and upper bounds of the error bars.

pch

The type of point to draw (see par).

cex

The size of the points to draw (see par).

lwd

The width of the lines (see par).

lty

The type of line to draw (see par).

arrow

Logical; if TRUE draws individual error bars while if FALSE draws a single filled bar.

length

The width of the caps on the error bars.

col

The color of the lines (see par).

col.p

The color of the points (see par).

col.eb

The color of the error bars.

bg

The background color of the points (see par).

border

The color for the border of a single filled error bar.

aes

An optional named character vector specifying column names (if x is a data frame) with values for pch, cex, lwd, lty, col, col.p, col.eb, and bg.

Value

Adds lines and error bars to an existing plot.

Examples

# Draw a line
plot_blank()
draw_lines( c( 0, .5, 1 ), c( 0, .5, 1 ) )

# Pass points in via data frame
draw_lines(
  data.frame( X = c( .1, .2, .3 ), Y = c( .8, .8, .8 ) ),
  lty = 2, lwd = 3, col.p = 'blue', col = 'blue'
)


# Compute mean, SE, and 95% CI limits for data set
dtf <- aggregate(
  mtcars$mpg, list( mtcars$cyl, mtcars$am ),
  function(x) c( mean(x), sem(x) )
)
dtf$X <- rep( 1:3, 2 )
dtf$M <- dtf$x[,1];
dtf$LB <- dtf$x[,1] - dtf$x[,2] * 1.96
dtf$UB <- dtf$x[,1] + dtf$x[,2] * 1.96

# Create blank plot
xl <- c( .5, 3.5 ); yl <- c( 5, 35 )
plot_blank( xl, yl )
draw_hv( h = yl, l = xl )
draw_hv( v = xl, l = yl )

# Draw lines for automatic transmission
draw_lines(
  dtf[1:3,],
  columns = c( 'X', 'M', 'LB', 'UB' )
)

# Draw lines for manual transmission
draw_lines(
  dtf[1:3 + 3,],
  columns = c( 'X', 'M', 'LB', 'UB' ),
  col = 'blue', col.p = 'blue', col.eb = col_to_hex( 'blue', .3 )
)

# Add axes and labels
draw_axes( 1:3, dtf$Group.1[1:3] )
mtext( 'Cylinders', side = 1, line = 2, cex = 1.25 )
draw_axes( c( 10, 20, 30 ), side = 2 )
mtext( 'MPG', side = 2, line = 2, cex = 1.25 )

# Add legend
legend(
  2.5, 30,
  c( 'Automatic', 'Manual' ),
  fill = c( 'black', 'blue' ),
  bty = 'n'
)