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!


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

Line types in R

! This guide was written using R version 3.4.2 on Windows 10.

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 type can be added as an argument lty= to your plot or graphics command. The type of line can be specified as either a number or a character string. You can also tell R not to draw a line using lty=0 or lty="blank". The default line type is "solid".

Lines types with "round" line ends

R line types List of the default line types available in R, and what they look like when plotted. This shows lines with round line ends. https://www.benjaminbell.co.uk 1. "solid" 2. "dashed" 3. "dotted" 4. "dotdash" 5. "longdash" 6. "twodash" Can't see the image? You need to upgrade your browser.

lty=1:6, lend=0


Lines types with "butt" line ends

R line types List of the default line types available in R, and what they look like when plotted. This shows lines with butt line ends. https://www.benjaminbell.co.uk 1. "solid" 2. "dashed" 3. "dotted" 4. "dotdash" 5. "longdash" 6. "twodash" Can't see the image? You need to upgrade your browser.

lty=1:6, lend=1


Lines types with "square" line ends

R line types List of the default line types available in R, and what they look like when plotted. This shows lines with square line ends. https://www.benjaminbell.co.uk 1. "solid" 2. "dashed" 3. "dotted" 4. "dotdash" 5. "longdash" 6. "twodash" Can't see the image? You need to upgrade your browser.

lty=1:6, lend=2

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

Lines end types (end caps)

Line end styles (end caps) can be specified using the lend= argument. There are three types available, which can be specified as a number or character string. The default line end type is "round".


R line end types 0. "round" 1. "butt" 2. "square" Can't see the image? You need to upgrade your browser.

As well as changing the line type and line end styles, you can also change the line colour col= and line width lwd=. For example:

#  Create Data
x1 <- runif(10, min=1, max=10)
x2 <- runif(10, min=1, max=10)
x3 <- runif(10, min=1, max=10)
# Plot
plot(x1, lty=1, type="l", col="red", lwd=6)
lines(x2, lty=2, col="blue", lwd=3)
lines(x3, lty=3, col="green", lwd=3)

Would result in the following plot:

Can't see the image? You need to upgrade your browser.
© 2018 Benjamin Bell. All Rights Reserved. https://www.benjaminbell.co.uk

Creating your own line types

You can also define your own line type styles, rather than using the default options. To do so, use a string of numbers, where the first number sets the number of "units" (e.g. the length) of the drawn line, and the second number sets the number of units where the line is not drawn. The "units" are relative to line width lwd=.

For example, lty=c("55") would draw a line consisting of equal lengths of drawn line to the space between the lines.

When defining your own line types, you must always enclose the numbers within quotation marks, otherwise R will use the default line type options. You can use up to four number pairs to create your line type.

Here's a few examples:

lty=c("19")

lty=c("2992")

lty=c("33185573")

© 2018 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. As a workaround, you can create multi-colour lines by plotting several 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")

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

Line join styles

There are three line join styles ljoin= which can be specified as either a number or character string. The default line join style is "round".

R line join types List of the default line join types available in R, and what they look like when plotted. https://www.benjaminbell.co.uk 0. "round" 1. "mitre" 2. "bevel" Can't see the image? You need to upgrade your browser.

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. This does not work on all systems (it has no effect on Windows).


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


Ad

© 2018 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