Line charts allow many different variations and some of them are shown with the help of the examples below.
One needs to understand that line plots require vectors of points on x,y plane to work. For the purpose of this tutorial, we will create two dummy vectors (one containing X points and one containing Y points.) of points and will use them throughout this page. Make sure you execute the code snippet (the first one) that includes the creation of dummy vectors before proceeding to use them in subsequent code snippets.
x <- c(1,2,3,4,5)
y <- c(7,4,3,1,6)
x
y
The above code snippet generates two vectors X and Y with some random points.
Now let us first simply plot the points (without any lines). The lines( ) function adds information to a graph. It can not produce a graph on its own. Usually, it follows a plot(x, y) command that produces a graph. By default, plot( ) plots the (x,y) points. Use the type="n" option in the plot( ) command, to create the graph with axes, titles, etc., but without plotting the points. Now we can use multiple variations on the 'type' arguments to produce different kinds of charts. The type="p" simply plots the points.
x <- c(1,2,3,4,5)
y <- c(7,4,3,1,6)
plot(x, y, type="n", main="Type p")
lines(x, y, type="p")
We can change the plotting symbol and the color of the plot using 'pch' argument (for the symbol) and 'col' argument (for color).
x <- c(1,2,3,4,5)
y <- c(7,4,3,1,6)
plot(x, y, type="n", main="Type p")
lines(x, y, type="p", pch=22, col="red")
Now let us plot a line chart using type="l"
x <- c(1,2,3,4,5)
y <- c(7,4,3,1,6)
plot(x, y, type="n", main="Type l")
lines(x, y, type="l", pch=22, col="red")
Try changing the value of the pch parameter and observe what happens.
We can use type="o" to plot overplotted points and lines.
x <- c(1,2,3,4,5)
y <- c(7,4,3,1,6)
plot(x, y, type="n", main="Type o")
lines(x, y, type="o", pch=22, col="red")
Lets plot the points joined by lines using type="b"
x <- c(1,2,3,4,5)
y <- c(7,4,3,1,6)
plot(x, y, type="n", main="Type b")
lines(x, y, type="b", pch=22, col="red")
Now we will use type="c" and observe what happens.
x <- c(1,2,3,4,5)
y <- c(7,4,3,1,6)
plot(x, y, type="n", main="Type c")
lines(x, y, type="c", pch=22, col="red")
Are you able to observe the subtle difference between type o, b, and c?
Now, let us plot stair steps using tye="s" parameter. (s is in lower case)
x <- c(1,2,3,4,5)
y <- c(7,4,3,1,6)
plot(x, y, type="n", main="Type s")
lines(x, y, type="s", pch=22, col="red")
Now let us use type="S" (s in upper case) and observe what happens.
x <- c(1,2,3,4,5)
y <- c(7,4,3,1,6)
plot(x, y, type="n", main="Type S")
lines(x, y, type="S", pch=22, col="red")
Observe the difference between using type s and S. The difference lies in where and when the transition of the step takes place.
Now, if you want to plot a histogram like vertical lines, then type="h" is used.
x <- c(1,2,3,4,5)
y <- c(7,4,3,1,6)
plot(x, y, type="n", main="Type h")
lines(x, y, type="h", pch=22, col="red")
And of course, we can add custom labels to the X and Y axis using the xlab and ylab parameters.
x <- c(1,2,3,4,5)
y <- c(7,4,3,1,6)
plot(x, y, type="n", main="Type h", xlab="X axis label", ylab="Y axis label")
lines(x, y, type="h", pch=22, col="red")
Now let us remove type="n" from the plot() function and see what happens.
x <- c(1,2,3,4,5)
y <- c(7,4,3,1,6)
plot(x, y, main="Type h")
lines(x, y, type="h", pch=22, col="red")
Can you observe the difference? Try removing the type="n" parameter from the plot() function in all the other examples in this page and see what happens.