Conditional Plotting Multiple Time Series with Lattice in R

Introduction

In this article, we will explore how to plot overlay multiple time series given conditions in lattice. Lattice is a powerful plotting library for R that allows us to create complex and customized plots with ease. We will delve into the world of lattice and see how it can be used to conditionally plot multiple time series.

Understanding the Problem

The problem at hand involves having a data frame df with a factor f and numerical values t1, t2, and t3 that represent time-ordered events. We want to overlay these time series on top of each other, but with some conditions applied based on the factor f. The goal is to create two panels, one for each condition (e.g., h and t), with the overlaid time series corresponding to those conditions.

Exploring the Data

Let’s take a closer look at the data:

  f t1 t2 t3 ID
h 1  3  4  4  1
h 2  4  3  3  2
t 3  4  5  6  3
t 5  6  8  7  4

Here, f represents the factor with values h and t, and t1, t2, and t3 represent the time-ordered events.

Non-Lattice Approach

One way to achieve this is by using a non-lattice approach. We can create two separate plots for each condition (e.g., f = "h" and f = "t") and then combine them later:

x <- matrix(seq(12), 4, 3)
f <- c("a", "a", "b", "b")
df <- data.frame(f, x)

layout(1:2)
yr <- c(0, 12)
xr <- c(1, 3)
plot(as.numeric(df[1, 2:4]) ~ seq(3), type = "o", ylim = yr, xlim = xr, ylab = "A")
par(new = T)
plot(as.numeric(df[2, 2:4]) ~ seq(3), type = "o", ylim = yr, xlim = xr, ylab = "A")

plot(as.numeric(df[3, 2:4]) ~ seq(3), type = "o", ylim = yr, xlim = xr, ylab = "B")
par(new = T)
plot(as.numeric(df[4, 2:4]) ~ seq(3), type = "o", ylim = yr, xlim = xr, ylab = "B")

However, this approach loses the row sequence connections and is not very flexible.

Using Lattice

Lattice provides a powerful way to conditionally plot multiple time series. We can use the xyplot function from the lattice package and specify the group parameter to keep the ID’s separate.

datm <- melt(df, id.vars = c("ID", "f"), measure.vars = c("t1", "t2", "t3"))
xyplot(value ~ variable | f, group = ID, data = datm, type = "b")

This code melts the original data frame into a long format using reshape2 and then uses lattice to plot the value against the variable for each condition.

Customization

The xyplot function provides several options for customization. We can change the type of plot by specifying the type parameter, which can be "b" for a line plot or "p" for a point plot.

xyplot(value ~ variable | f, group = ID, data = datm, type = "b")

We can also change the color scheme by using the col parameter.

Example Use Cases

Here are some example use cases for conditioningally plotting multiple time series with lattice:

  • Suppose we have a dataset of daily sales figures for two different products, and we want to compare their sales patterns over time. We can conditionally plot the sales figures on top of each other using xyplot.
  • Suppose we have a dataset of website traffic figures for three different days of the week, and we want to compare their traffic patterns on each day. We can conditionally plot the traffic figures on top of each other using xyplot.

Conclusion

Conditionally plotting multiple time series with lattice provides a powerful way to visualize complex data in R. By using the xyplot function from the lattice package, we can create customized plots that show how different conditions affect our data. This is particularly useful when working with datasets that have multiple variables or categories.

We hope this article has provided you with a better understanding of how to conditionally plot multiple time series with lattice in R. With practice and experience, you’ll be able to create beautiful and informative plots that showcase your data in the most effective way possible.


Last modified on 2023-11-30