Pages (Desktop)

Pages (Mobile)

Quick guide to line types (lty) in R

A quick guide to the different line types that are available to use in R, and how to use them. This guide will also show you how to create your own line type, and additionally covers line end styles (lend) and line join styles (ljoin).

A handy reference for lty, lend and ljoin!

Guide Information

Title Quick guide to line types (lty) in R
Author Benjamin Bell
Published February 26, 2018
Last updated August 12, 2021
R version 3.4.2
Packages base

This guide has been updated:

  • This guide has been rewritten to provide more detailed information, clearer examples, and better visualisation of the different line types and customisations.

Customising lines in R

There are several options available to customise how R draws lines on plots. These include the end caps lend, type of line lty, line width lwd, line colour col, the type of join ljoin, and even the mitre limit lmitre. These arguments can be applied to almost any function that draws lines, including: plot(), lines() and abline().

Line end caps

There are three options available for customising line end caps in R using the lend argument. You can use either the number or the name of the style, for example:

# Plot window to draw lines
plot.new()
# Plot coordinates to draw line
x <- c(0.25, 0.75)
y <- c(0.5, 0.5)
# Specify line end cap using numbers
lines(x, y, lend=0)
# Specify line end cap using the name
lines(x, y, lend="round")

In both of these examples, the lines would be drawn with a round end cap (this is also the default value). The other options are: 1 or "butt" and 2 or "square" (see below).

lend=0
lend="round"
lend=1
lend="butt"
lend=2
lend="square"
© Benjamin Bell. All Rights Reserved. https://www.benjaminbell.co.uk

Line types

There are six pre-defined line types available in base R. You can use these for any type of graphics, whether it is for plotting line charts or creating simple shapes. Line types can be customised by using the lty argument.

Line types can be specified using either the number or the name of the line type, for example:

# Specify line type using numbers
lines(x, y, lty=2)
# Specify line type using the name
lines(x, y, lty="dashed")

In both of these examples, R would draw a dashed line in place of a solid line (which is the default).

Line types with "round" line end caps

lty=1
lty="solid"
lty=2
lty="dashed"
lty=3
lty="dotted"
lty=4
lty="dotdash"
lty=5
lty="longdash"
lty=6
lty="twodash"

Line types with "butt" line end caps

lty=1
lty="solid"
lty=2
lty="dashed"
lty=3
lty="dotted"
lty=4
lty="dotdash"
lty=5
lty="longdash"
lty=6
lty="twodash"

Line types with "square" line end caps

lty=1
lty="solid"
lty=2
lty="dashed"
lty=3
lty="dotted"
lty=4
lty="dotdash"
lty=5
lty="longdash"
lty=6
lty="twodash"

By default, line types will be drawn with round line end caps. If you want to use either "butt" or "square" line end caps with a different line type, you must specify both arguments. For example:

# "dotted" line with "butt" line end caps
lines(x, y, lty=3, lend=1)
lines(x, y, lty="dotted", lend="butt")
# "twodash" line with "square" line end caps
lines(x, y, lty=6, lend=2)
lines(x, y, lty="twodash", lend="square")

You can also specify the line type as "blank" (or 0) which tells R not to draw the line.

# Do not draw a line
lines(x, y, lty=0)
lines(x, y, lty="blank")
© Benjamin Bell. All Rights Reserved. https://www.benjaminbell.co.uk

Creating your own line types

It is possible to define your own line type, rather than using the default types. To do so, you need to use a string of numbers with a minimum of two numbers (one pair), and a maximum of eight (four pairs). In each number pair, the first number defines the number of "units" (or length) to draw the line (or the dash), while the second number defines the number of units where a line is not drawn (or the gap). The units are relative to the line width lwd.

The following example will create a custom line type with equal length dashes and gaps:

# Custom line type with equal lengths
lines(x, y, lty=c("88"))

So why do the gaps appear to be a different length then the dashes? This is because "round" line end caps (the default) extend beyond the length of the line (this also occurs with "square" line end caps).

Compare the same custom line type, but with "butt" line end caps:

# Custom line type with equal lengths
lines(x, y, lty=c("88"), lend=3)

Now the dashes and gaps appear as the same size.

The length of the dash and gap will also change if you change the line width, for example:

# Custom line type with equal lengths
lines(x, y, lty=c("88"), lwd=10)

In the following example, two number pairs are used to produce a custom line:

# Custom line type with long dash and short space
lines(x, y, lty=c("8822"))

Here, we can break down the code to illustrate how this custom line is created.

# Custom line type with equal lengths
# First dash with length of 8 units
lines(x, y, lty=c("8822"))
# First gap with length of 8 
lines(x, y, lty=c("8822"))
# Second dash with length of 2
lines(x, y, lty=c("8822"))
# Second gap with length of 2
lines(x, y, lty=c("8822"))
Line type Repeat of line type First dash with length of 8 First gap with length of 8 Second dash with length of 2 Second gap with length of 2 benjaminbell.co.uk

When setting the length of the dashes and gaps, you can use any value between 1 and 9, and you must always enclose the custom line type in quotation marks.

Here are a few more examples:

lty=c("19")

lty=c("2992")

lty=c("33185573")

© Benjamin Bell. All Rights Reserved. https://www.benjaminbell.co.uk

Line join styles

There are three line join styles ljoin that can be used when drawing lines in R. The default is "round", for example:

# Plot window to draw lines
plot.new()
# Random data
x <- c(0, 0.3, 0.6, 0.9)
y <- c(0.3, 0.7, 0.3, 0.7)
# Round line joins
lines(x, y, ljoin=0)
lines(x, y, ljoin="round")
# Mitred line joins
lines(x, y, ljoin=1)
lines(x, y, ljoin="mitre")
# Bevelled line joins
lines(x, y, ljoin=2)
lines(x, y, ljoin="bevel")
ljoin=0
ljoin="round"
ljoin=1
ljoin="mitre"
ljoin=2
ljoin="bevel"

Line join styles can also be applied to shapes. For example:

# Empty plot to draw shapes in
plot.new()
# Square with...
rect(0, 0, 1, 1, lwd=20) # Round corners
rect(0, 0, 1, 1, lwd=20, ljoin=1) # Mitred corners
rect(0, 0, 1, 1, lwd=20, ljoin=2) # Bevelled corners

Round Mitre Bevel

There is a bug on Windows systems where the windows() graphics device incorrectly displays the "final" line join as the default style, "round".

To ensure the plot is displayed correctly, Windows users should use a different graphics device, such as pdf() when saving the plot. MacOS and Linux systems are not affected by this bug.

For "mitre" line join styles, you can also adjust the mitre limit using lmitre, and specifying a number greater than 1. The default is 10. Increasing the value means that mitred line joins are less likely to become bevelled joins. However, many factors influence how this argument works, thus the value you assign to it appears to be somewhat arbitrary. Additionally, this argument does not work on all systems (it has no effect on Windows).


© Benjamin Bell. All Rights Reserved. https://www.benjaminbell.co.uk

Multi-colour lines

It is not possible to specify different colours for the same dashed line in R. However, as a workaround, you can create multi-colour lines by plotting several different lines on top of each other.

For example, the following code would plot an alternate black and yellow line:

# Create blank plot window
plot(0, xlim=c(1,4), ylim=c(1,4)) 
# Create lines
abline(h=1, lwd=4, lty=1, lend=1, col="yellow")
abline(h=1, lwd=4, lty=2, lend=1, col="black")

You could get quite creative...

abline(h=1, lwd=8, lty=1, lend=1, col="red")
abline(h=1, lwd=8, lty=c("84"), lend=1, col="white")
abline(h=1, lwd=8, lty=c("48"), lend=1, col="blue")

abline(h=1, lwd=4, lty=1, lend=1, col="limegreen")
abline(h=1, lwd=4, lty=c("22"), lend=1, col="skyblue")
abline(h=1, lwd=4, lty=c("24"), lend=1, col="orange")
abline(h=1, lwd=4, lty=c("26"), lend=1, col="purple")
abline(h=1, lwd=4, lty=c("28"), lend=1, col="hotpink")

abline(h=1, lwd=10, lty=1, lend=1, col="black")
abline(h=1, lwd=6, lty=c("22"), lend=0, col="yellow")
abline(h=1, lwd=6, lty=c("24"), lend=0, col="green")

Thanks for reading this guide! Please leave any comments or questions below.

© Benjamin Bell. All Rights Reserved. http://www.benjaminbell.co.uk

Further reading

A quick guide to pch symbols - A quick guide to the different pch symbols which are available in R, and how to use them. [R Graphics]

A quick guide to layout() in R - How to create multi-panel plots and figures using the layout() function. [R Graphics]

No comments

Post a Comment

Comments are moderated. There may be a delay until your comment appears.