Skip to contents

Draws one or several line series using graphics::matplot(). The function accepts either a matrix of values or separate x and y coordinates and supports optional point symbols, grid lines, and an automatically positioned legend.

Usage

plotLines(x, ...)

# Default S3 method
plotLines(
  x,
  y,
  main = NULL,
  xlab = "",
  ylab = "",
  xlim = NULL,
  ylim = NULL,
  xax = NULL,
  yax = NULL,
  lty = 1,
  lwd = 2,
  col = .useTheme,
  points = FALSE,
  grid = .useTheme,
  legend = TRUE,
  stamp = .useTheme,
  ...
)

# S3 method for class 'formula'
plotLines(formula, data = parent.frame(), subset, na.action, main = NULL, ...)

Arguments

x

numeric vector, matrix or data frame. If y is missing, x is interpreted as a matrix of series where rows correspond to x positions and columns to individual lines.

...

additional graphical parameters passed to graphics::par() via .applyParFromDots() and to the plotting functions.

y

optional numeric vector or matrix giving the y-values. If supplied, x is interpreted as the x-coordinates.

main

main title. NULL (default) draws none, unless the theme sets autoTitle = TRUE (see theme); the title is then derived from the input. "", NA or FALSE always suppress it and compact the top margin; any other string is used as given.

xlab, ylab

labels for the axes.

xlim, ylim

limits for the axes.

xax, yax

control the x- and the y-axis. NULL (default) draws the axis with default settings, FALSE suppresses it. A string is taken as the format of the tick labels, yax = "%" is short for yax = list(fmt = "%"). A named list may mix arguments for fm() (fmt, digits, bigMark, ...) and for graphics::axis() (las, col, cex, ...), e.g. yax = list(fmt = "%", digits = 0). See Details for a date axis and for row names.

lty

line type(s).

lwd

line width(s).

col

colours for the lines. .useTheme (default) resolves to pal(getTheme()$palette), the active theme's qualitative palette.

points

controls drawing of points on the lines. FALSE (default) suppresses points; TRUE draws with theme defaults (getTheme()$points); a named list overrides individual elements (pch, col, bg, cex).

grid

controls drawing of the background grid. .useTheme (default) follows the active theme (getTheme()$grid). TRUE/FALSE/NA, or a named list, as for graphics::grid().

legend

controls the legend. TRUE (default) draws an inline legend via textLegend at the last value of each series. FALSE/NA suppresses it. A list overrides legend arguments.

stamp

controls the corner stamp. .useTheme (default) resolves to getTheme()$stamp. TRUE/FALSE/NULL, a string, or a named list for stamp().

formula

a formula y ~ x. Several series are given as y1 + y2 ~ x or cbind(y1, y2) ~ x; the left-hand side terms become the legend labels.

data

a data frame (or list) in which the variables of formula are evaluated.

subset

an optional expression selecting the rows of data.

na.action

a function handling NAs. Defaults to stats::na.pass(), so that missing values leave gaps in the lines.

Value

Invisibly returns a list containing:

  • x the x-values used for plotting,

  • y the y-values (if supplied),

  • legend the legend specification if drawn.

Details

If y is missing, x is interpreted as a matrix and each column is drawn as a separate line. Row names are used for the x-axis labels if available; they are category names, so the format arguments in xax do not apply to them, its axis() arguments do. The legend labels default to the column names of the data.

If x is a Date or POSIXct vector, the x-axis ticks fall on calendar boundaries (years, months, days, ...) and the date patterns of fm() can be used to label them, e.g. xax = "yyyy" or xax = list(fmt = "MMM yy").

Examples

m <- matrix(c(3,4,5,1,5,4,2,6,2), nrow = 3,
            dimnames = list(
              dose = c("A","B","C"),
              age  = c("2000","2001","2002")
            ))

plotLines(m, lwd = 2, main = "Dose ~ Age")


# with points
plotLines(m, points = TRUE)


# custom legend
plotLines(m, legend = list(cex = 0.8))


# formatted axes: shares in percent over a date axis
d <- seq(as.Date("2011-01-01"), by = "year", length.out = 9)
s <- prop.table(matrix(runif(27, 1, 5), ncol = 3,
                       dimnames = list(NULL, c("A", "B", "C"))), 1)
plotLines(d, s, yax = list(fmt = "%", digits = 0), xax = "yyyy")


# formula interface
df <- data.frame(year = 2014:2025, a = cumsum(runif(12)), b = cumsum(runif(12)))
plotLines(a ~ year, df)

plotLines(a + b ~ year, df, subset = year >= 2018)