Line types and styles in R

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.

Line types

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.

Line types in R

πŸ‘‰ Example: "55" creates a pattern of 5 units followed by 5 blank units. Units are proportional to lwd.

Line widths

Line widths in R

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.

Line plot types

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

Plot line types in R

Line colors

Color of the lines in R

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)

Color line pattern in R

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)

Line end and join style

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.

Line end types in R with lend argument

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

Line join types in R with ljoin argument

Note that the red lines represent the actual start and end of the data.

Storytelling with Data

A Data Visualization Guide for Business Professionals

Buy on Amazon
ggplot2

Elegant Graphics for Data Analysis

Buy on Amazon
Data Sketches

A journey of imagination, exploration, and beautiful data visualizations

Buy on Amazon
Fundamentals of Data Visualization

A Primer on Making Informative and Compelling Figures

Buy on Amazon
Better Data Visualizations

A Guide for Scholars, Researchers, and Wonks

Buy on Amazon

See also