Lines graph, also known as line charts or line plots, display ordered data points connected with straight segments. In this tutorial you will learn how to plot line graphs in base R using the plot, lines, matplot, matlines and curve functions and how to modify the style of the resulting plots.
A line chart can be created in base R with the plot function. Consider that you have the data displayed on the table below:
| x | y |
|---|---|
| 1 | 200 |
| 2 | 400 |
| 3 | 600 |
| 4 | 700 |
| 5 | 500 |
You can plot the previous data using three different methods:
specifying the two vectors, passing the data as data frame or with a
formula. Note that we set type = "l" to connect the data
points with straight segments.
# Data
x <- c(1, 2, 3, 4, 5)
y <- c(200, 300, 600, 700, 500)
# Vectors
plot(x, y, type = "l")
# Data frame
plot(data.frame(x, y), type = "l") # Equivalent
# Formula
plot(y ~ x, type = "l") # Equivalent
The style of the line graphs in R can be customized with the
arguments of the function. As an example, the color and line width can
be modified using the col and lwd arguments,
respectively.
plot(x, y, type = "l",
col = "lightblue", # Color
lwd = 5) # Line width
Besides type = "l", there are three more types of line
graphs available in base R. Setting type = "s" will create
a stairs line graph, type = "b" will create a line plot
with segments and points and type = "o" will also display
segments and points, but with the line overplotted.
par(mfrow = c(1, 3))
plot(x, y, type = "s", main = 'type = "s"')
plot(x, y, type = "b", main = 'type = "b"')
plot(x, y, type = "o", main = 'type = "o"')
par(mfrow = c(1, 1))
Furthermore, there exist six different types of lines, that can be
specified making use of the lty argument, from 1 to 6:
par(mfrow = c(2, 3))
plot(x, y, type = "l", lwd = 2, lty = 1, main = "lty = 1")
plot(x, y, type = "l", lwd = 2, lty = 2, main = "lty = 2")
plot(x, y, type = "l", lwd = 2, lty = 3, main = "lty = 3")
plot(x, y, type = "l", lwd = 2, lty = 4, main = "lty = 4")
plot(x, y, type = "l", lwd = 2, lty = 5, main = "lty = 5")
plot(x, y, type = "l", lwd = 2, lty = 6, main = "lty = 6")
par(mfrow = c(1, 1))
You can also customize the symbol used when type = "b"
or type = "o". These symbols, also known as pch symbols can
be selected with the pch argument, that takes values from 0
(square) to 25. See pch
symbols for more information. Some of the available symbols are the
following:
The color of the symbol can be specified with the col
argument, that will also modify the color of the line.
plot(x, y, type = "b", cex = 2, pch = 21, bg = "blue", col = "red")
However, you can also add the points separately using the
points function. This approach will allow you to customize
all the colors as desired.
plot(x, y, type = "l", col = "red")
# Adding points
points(x, y, # Coordinates
pch = 21, # Symbol
cex = 2, # Size of the symbol
bg = "green", # Background color of the symbol
col = "blue", # Border color of the symbol
lwd = 3) # Border width of the symbol
Note that the pch argument also allow to input
characters, but only one. In the following example we are passing the
first five letters of the alphabet.
plot(x, y, type = "b",
pch = LETTERS[1:5], # Letters as symbols
cex = 2, # Size of the symbols
col = 1:5, # pch colors
xlim = c(0, 6), # X-axis limits
ylim = c(150, 750)) # Y-axis limits
In case you need to make some annotations to the chart you can use
the text function, which first argument is the X
coordinate, the second the Y coordinate and the third the
annotation.
plot(x, y, type = "l")
text(x = 3, y = 650, "Custom annotation")
In the previous section we reviewed how to create a line chart from
two vectors, but in some scenarios you will need to create a line plot
of a function. For that purpose you can use the curve
function, specifying the function and the X-axis range with the
arguments from and to.
curve(cos, from = 0, to = 10, ylab = "", main = "Sine and cosine")
# New curve over the first
curve(sin, from = 0, to = 10,
col = 2,
add = TRUE) # Needed to add the curve over the first
Note that you can also create a line plot from a custom function:
# Custom function
fun <- function(x){
return(x ^ 3)
}
# Plot the custom function
curve(fun, from = -5, to = 5, ylab = expression(x^3),
main = "curve function")
If you have more variables you can add them to the same plot with the
lines function. As an example, if you have other variable
named y2, you can create a line graph with the two
variables with the following R code:
# More data
y2 <- c(300, 400, 450, 400, 250)
# First line
plot(x, y, type = "l")
# Second line
lines(x, y2, type = "l", col = 2) # Same X values
Note that the lines function is not designed to create a
plot by itself, but to add a new layer over a already created plot.
A better approach when dealing with multiple variables inside a data
frame or a matrix is the matplot function. Considering that
you have the following multivariate normal data:
# install.packages("MASS")
library(MASS) # For the mvrnorm function
set.seed(1)
# Multivariate normal data
means <- rep(0, 5)
variances <- matrix(1:25, ncol = 5)
data <- data.frame(mvrnorm(n = 10, mu = means, Sigma = variances))
# First six rows
head(data)
## X1 X2 X3 X4 X5
## 1 1.11120719 0.06637579 1.092223 1.159993 5.55603597
## 2 -0.01699271 -0.06022054 -1.352031 -1.334390 -0.08496353
## 3 0.69497073 2.37299879 3.724160 3.220189 3.47485367
## 4 -2.24610790 -3.08992821 -2.351085 -5.215908 -11.23053948
## 5 -0.07374956 -0.66577823 -3.467711 -1.019625 -0.36874780
## 6 0.75633742 2.02488355 2.533323 3.772040 3.78168712
You can plot all the columns at once with the function:
# Plot all columns at once
matplot(data, type = "l", main = "matplot function")
Equivalently to the lines function,
matlines allows adding new lines to an existing plot. For
instance, you can plot the first three columns of the data frame with
the matplot function and then add the last two with
matlines.
# Three first columns of the data frame
data1 <- data[, 1:3]
# Plot the three columns at once
matplot(data1, type = "l", lty = 1,
ylab = "data",
ylim = c(min(data), max(data))) # Y-axis limits
In addition to creating line charts with numerical data, it is also possible to create them with a categorical variable. Consider the following sample data:
# Data
data <- data.frame(group = as.factor(c("Group 1", "Group 2", "Group 3")),
var1 = c(1, 3, 2),
var2 = c(2, 1.5, 1.75))
head(data)
## group var1 var2
## 1 Group 1 1 2.00
## 2 Group 2 3 1.50
## 3 Group 3 2 1.75
If you want to plot the data as a line graph in R you can transform
the factor variable into numeric with the is.numeric
function and create the plot. You can set the factor variable on the
X-axis or on the Y-axis:
par(mfrow = c(1, 2))
#-----------------
# Groups on X-axis
#-----------------
plot(as.numeric(data$group), data$var1, type = "l",
ylab = "Value", xlab = "Group",
xaxt = "n")
# Second variable
lines(as.numeric(data$group), data$var2, col = 2)
# Group names
axis(1, labels = as.character(data$group), at = as.numeric(data$group))
#-----------------
# Groups on Y-axis
#-----------------
plot(data$var1, as.numeric(data$group), type = "l",
ylab = "Group", xlab = "Value",
yaxt = "n")
# Second variable
lines(data$var2, as.numeric(data$group), col = 2)
# Group names
axis(2, labels = as.character(data$group), at = as.numeric(data$group))
par(mfrow = c(1, 1))
The legend function allows adding legends in base R plots. You just need to specify the position or the coordinates, the labels of the legend, the line type and the color. You can also specify a pch symbol if needed.
plot(x = 1:10, y = 1:10, type = "l")
lines(x = 1:10, y = sqrt(1:10), col = 2, type = "l")
legend("topleft", legend = c("line 1", "line2"), lty = 1, col = 1:2)
Finally, it is important to note that you can add a second axis with the axis function as follows:
# Increase the plot margins
par(mar = c(5.25, 4.25, 4.25, 4.25))
# First line
plot(x = 1:10, y = 1:10, type = "l", xlab = "x", ylab = "Line 1")
# New plot (needed to merge both plots)
par(new = TRUE)
# Second line
plot(1:10, (1:10)^2, type = "l",
col = 2,
axes = FALSE, # No axes
bty = "n", # No box
xlab = "", ylab = "")
# New axis
axis(4)
mtext("Line 2", side = 4, line = 3, col = 2)
reference : https://r-coder.com/line-graph-r/