Draw Multiple Time Series in Same Plot in R (2 Examples)
In this article you’ll learn how to create a plot showing multiple time series in the R programming language.
The post contains the following topics:
Let’s get started.
Creation of Example Data
First, we’ll have to construct some data that we can use in the examples below:
set.seed(1023172) # Create random example data data <- round(data.frame(year = 2001:2025, ts1 = 1:25 + rnorm(25), ts2 = 30:6 + runif(25, 0, 10), ts3 = rnorm(25, 5, 5))) head(data) # Head of example data # year ts1 ts2 ts3 # 1 2001 1 33 9 # 2 2002 1 31 0 # 3 2003 2 31 -9 # 4 2004 4 33 4 # 5 2005 6 33 4 # 6 2006 6 35 5 |
set.seed(1023172) # Create random example data data <- round(data.frame(year = 2001:2025, ts1 = 1:25 + rnorm(25), ts2 = 30:6 + runif(25, 0, 10), ts3 = rnorm(25, 5, 5))) head(data) # Head of example data # year ts1 ts2 ts3 # 1 2001 1 33 9 # 2 2002 1 31 0 # 3 2003 2 31 -9 # 4 2004 4 33 4 # 5 2005 6 33 4 # 6 2006 6 35 5
The previous output of the RStudio console shows that our example data has four columns. The variable year defines the time range and the variables ts1, ts2 and ts3 contain the corresponding values of three different time series.
Example 1: Drawing Multiple Time Series in Base R
In Example 1, I’ll illustrate how to draw a graph showing multiple time series using the basic installation of the R programming language.
More precisely, we have to use the plot, lines, and legend functions as follows:
plot(data$year, # Draw first time series data$ts1, type = "l", col = 2, ylim = c(- 15, 40), xlab = "Year", ylab = "Values") lines(data$year, # Draw second time series data$ts2, type = "l", col = 3) lines(data$year, # Draw third time series data$ts3, type = "l", col = 4) legend("topright", # Add legend to plot c("ts1", "ts2", "ts3"), lty = 1, col = 2:4) |
plot(data$year, # Draw first time series data$ts1, type = "l", col = 2, ylim = c(- 15, 40), xlab = "Year", ylab = "Values") lines(data$year, # Draw second time series data$ts2, type = "l", col = 3) lines(data$year, # Draw third time series data$ts3, type = "l", col = 4) legend("topright", # Add legend to plot c("ts1", "ts2", "ts3"), lty = 1, col = 2:4)
As shown in Figure 1, we created a time series graphic containing multiple lines with the previous syntax.
Example 2: Drawing Multiple Time Series Using ggplot2 Package
In Example 2, I’ll show how to plot multiple time series to a graph using the ggplot2 package in R.
The ggplot2 package typically takes long data as input. For that reason, we first have to use the reshape2 package to convert our data frame from wide to long format.
We first need to install and load the reshape2 package, if we want to use the functions that are included in the add-on package:
install.packages("reshape2") # Install reshape2 package library("reshape2") # Load reshape2 package |
install.packages("reshape2") # Install reshape2 package library("reshape2") # Load reshape2 package
Now, we can reshape our data from wide to long format using the melt function:
data_long <- melt(data, id.vars = "year") # Reshaping data to long format head(data_long) # Head of long data # year variable value # 1 2001 ts1 1 # 2 2002 ts1 1 # 3 2003 ts1 2 # 4 2004 ts1 4 # 5 2005 ts1 6 # 6 2006 ts1 6 |
data_long <- melt(data, id.vars = "year") # Reshaping data to long format head(data_long) # Head of long data # year variable value # 1 2001 ts1 1 # 2 2002 ts1 1 # 3 2003 ts1 2 # 4 2004 ts1 4 # 5 2005 ts1 6 # 6 2006 ts1 6
Furthermore, we need to install and load the ggplot2 package to draw our time series plot using ggplot2:
install.packages("ggplot2") # Install ggplot2 package library("ggplot2") # Load ggplot2 package |
install.packages("ggplot2") # Install ggplot2 package library("ggplot2") # Load ggplot2 package
Finally, we can apply the ggplot and geom_line commands to draw multiple time series to a plot:
ggplot(data_long, # Draw ggplot2 time series plot aes(x = year, y = value, col = variable)) + geom_line() |
ggplot(data_long, # Draw ggplot2 time series plot aes(x = year, y = value, col = variable)) + geom_line()
As shown in Figure 2, the previous syntax created a graphic showing multiple time series variables.
Video, Further Resources & Summary
In case you need further info on the R programming code of this article, you may have a look at the following video of my YouTube channel. In the video, I explain the R programming syntax of this article.
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
Furthermore, I can recommend reading the related tutorials of this homepage.
- Draw Multiple Graphs & Lines in Same Plot
- Draw Multiple Variables as Lines to Same ggplot2 Plot
- Draw Time Series Plot with Events Using ggplot2 Package
- Drawing Plots in R
- All R Programming Examples
In this article you learned how to print a graph with multiple overlaying time series and a legend in the R programming language. Don’t hesitate to let me know in the comments section below, in case you have additional questions. Furthermore, don’t forget to subscribe to my email newsletter in order to get updates on new tutorials.
Statistics Globe Newsletter