What are the Different R Line Types?

In R, sometimes, we need some modifications to a plot that we want to plot such as line width, line type, line end style, etc. In this article, we will look at all these parameters and their use in modifying plots for convenience. These arguments can be set in the plot() function. We will look at each argument and examples to show how the changes in these arguments affect the plotted line.

Line Width

The line width can be set using ‘lwd’ to define the line width. The default value is 1. To make a thinner line, the value should be less than 1 and for a thicker line, the value should be more than 1. The value has to be a positive number.

Let us look at some examples to demonstrate.

What are the Different R Line Types? -magoosh

Line end style

The line end style is set using ‘lend’ argument. There are three kinds of line end styles to choose from. They can be set by using names or numbers as follows:

  • 0 – “round”
  • 1 – “butt”
  • 2 – “square”

Let us have a look at how these affect the plot.

Line Join Style

The line join style is modified using ‘ljoin’. Like line end type, these can set by using names or numbers and has three kinds to choose from. These are:

  • 0 – “round”
  • 1 – “mitre”
  • 2 – “bevel”

Let us look at the implementation in R.

Input – plot(c(0, 1, 1.5, 2, 3), c(0, 0, 5, 0, 0), type=”l”, lwd=10, axes=FALSE, xlab=NA, ylab=NA, ljoin=0)

Input – plot(c(0, 1, 1.5, 2, 3), c(0, 0, 5, 0, 0), type=”l”, lwd=10, axes=FALSE, xlab=NA, ylab=NA, ljoin=1)

Input – plot(c(0, 1, 1.5, 2, 3), c(0, 0, 5, 0, 0), type=”l”, lwd=10, axes=FALSE, xlab=NA, ylab=NA, ljoin=2)

Line Type

It is used to set the type of line. There are seven pre-set types of lines that are available in R. They can be set by inputting either integer or name in ‘lty’ argument. They are as follows:

  • 0 – blank
  • 1 – solid
  • 2 – dashed
  • 3 – dotted
  • 4 – dotdash
  • 5 – longdash
  • 6 – twodash

Let us look at each of them in R.

Input – plot(c(0,1), c(0,0), type=”l”, axes=FALSE, xlab=NA, ylab=NA, lty=0)

Input – plot(c(0,1), c(0,0), type=”l”, axes=FALSE, xlab=NA, ylab=NA, lty=1)

Input – plot(c(0,1), c(0,0), type=”l”, axes=FALSE, xlab=NA, ylab=NA, lty=2)

Input – plot(c(0,1), c(0,0), type=”l”, axes=FALSE, xlab=NA, ylab=NA, lty=3)

Input – plot(c(0,1), c(0,0), type=”l”, axes=FALSE, xlab=NA, ylab=NA, lty=4)

Input – plot(c(0,1), c(0,0), type=”l”, axes=FALSE, xlab=NA, ylab=NA, lty=5)

Input – plot(c(0,1), c(0,0), type=”l”, axes=FALSE, xlab=NA, ylab=NA, lty=6)

Summary

In this article, we looked at various arguments to modify the line in a plot. These tools help us to plot better, clearer graphs to improve data visualization. Better plots help us understand and interpret data better and is a helpful tool in data analytics.

Comments are closed.


Magoosh blog comment policy: To create the best experience for our readers, we will only approve comments that are relevant to the article, general enough to be helpful to other students, concise, and well-written! 😄 Due to the high volume of comments across all of our blogs, we cannot promise that all comments will receive responses from our instructors.

We highly encourage students to help each other out and respond to other students' comments if you can!

If you are a Premium Magoosh student and would like more personalized service from our instructors, you can use the Help tab on the Magoosh dashboard. Thanks!