Lines (or curves) can be customized in R in several ways with different graphical parameters. The graphical parameters of this guide can be used with several functions such as lines
, curve
, matlines
, segments
, abline
and arrows
.
Built-in types
There exist seven styles, specified by either their integer or character string. The default value is 1 ("solid"
).
String types
You can also specify patterns with strings of 2, 4, 6, or 8 characters (hexadecimal, characters, 1β9 and aβf) to create custom lines.
π Example: "55"
creates a pattern of 5 units followed by 5 blank units. Units are proportional to lwd
.
The width of the lines can be set using the lwd
argument. The default value is 1.
You can set any positive value in order to create the desired line.
When creating a plot with the plot
or matplot
or when adding new lines with lines
and matlines
functions you can create different types of line plots with the type
argument. Defaults to "p"
(points).
Type | Description |
---|---|
βpβ | Points |
βlβ | Lines |
βbβ | Lines and points |
βcβ | Lines without the part of the points |
βoβ | Lines and points (overplotted) |
βhβ | Histogram-like |
βsβ | Stairs (first line horizontal) |
βSβ | Stairs (first line vertical) |
βnβ | No plotting |
You can customize the colors of the lines with the col
argument. See the colors section to know more about the available colors.
data <- matrix(1:40, ncol = 8)
matplot(data, type = "l",
col = 1:10, lty = 1, lwd = 2)
Patterns
π‘ Tip: combine line types and colors to create custom patterns.
curve(cos, -4, 4, col = 4, lwd = 7,
lty = 1)
curve(cos, -4, 4, col = 2, lwd = 7,
lty = "aa",
add = TRUE)
lend
There are three different line end styles, that can be selected with the lend
argument by either their integer (0, 1, 2) or character string ("round"
, "butt"
and "square"
, respectively). Defaults to 0.
ljoin
You can also select between different line join styles with the ljoin
argument, which possible values are 0 or "round"
(default), 1 or "mitre"
and 2 or "bevel"
.
Note that the red lines represent the actual start and end of the data.
See also