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
yis missing,xis 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,
xis interpreted as the x-coordinates.- main
main title.
NULL(default) draws none, unless the theme setsautoTitle = TRUE(see theme); the title is then derived from the input."",NAorFALSEalways 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,FALSEsuppresses it. A string is taken as the format of the tick labels,yax = "%"is short foryax = list(fmt = "%"). A named list may mix arguments forfm()(fmt,digits,bigMark, ...) and forgraphics::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 topal(getTheme()$palette), the active theme's qualitative palette.- points
controls drawing of points on the lines.
FALSE(default) suppresses points;TRUEdraws 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 forgraphics::grid().- legend
controls the legend.
TRUE(default) draws an inline legend viatextLegendat the last value of each series.FALSE/NAsuppresses it. A list overrides legend arguments.- stamp
controls the corner stamp.
.useTheme(default) resolves togetTheme()$stamp.TRUE/FALSE/NULL, a string, or a named list forstamp().- formula
a formula
y ~ x. Several series are given asy1 + y2 ~ xorcbind(y1, y2) ~ x; the left-hand side terms become the legend labels.- data
a data frame (or list) in which the variables of
formulaare evaluated.- subset
an optional expression selecting the rows of
data.- na.action
a function handling
NAs. Defaults tostats::na.pass(), so that missing values leave gaps in the lines.
Value
Invisibly returns a list containing:
xthe x-values used for plotting,ythe y-values (if supplied),legendthe 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").
See also
Other plot.univariate:
plotArea(),
plotBar(),
plotBox(),
plotCatDist(),
plotDens(),
plotDensBox(),
plotDot(),
plotECDF(),
plotFreqDist(),
plotQQ(),
plotRidge(),
plotViolin()
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)
