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`

!

## Line types in R

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

`lty=1:6`

, `lend=0`

### Lines types with "butt" line ends

`lty=1:6`

, `lend=1`

### Lines types with "square" line ends

`lty=1:6`

, `lend=2`

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

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:

## 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")`

## 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")
```

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

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

## 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