Unveiling the Time Traveler: Plotting Time Series in R

[This article was first published on Steve's Data Tips and Tricks, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Introduction

Ready to journey through time with R? Buckle up, because we’re about to explore the art of visualizing time-dependent data, known as time series analysis. Whether you’re tracking monthly sales patterns or analyzing yearly climate trends, R has your back with powerful tools to visualize these stories through time.

Our Flight Plan:

  1. Loading Up with Data: Grabbing our trusty dataset, AirPassengers.
  2. Taking Off with Base R: Creating a basic time series plot using base R functions.
  3. Soaring with ggplot2: Crafting a visually stunning time series plot using the ggplot2 library.
  4. Navigating Date Formatting: Customizing axis labels with scale_x_date() for clarity.
  5. Landing with Your Own Exploration: Encouraging you to take the controls and create your own time series plots!

1. Ready for Takeoff: Loading Data

We’ll start by loading the built-in AirPassengers dataset, which chronicles monthly passenger totals from 1949 to 1960:

AirPassengers
     Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
1949 112 118 132 129 121 135 148 148 136 119 104 118
1950 115 126 141 135 125 149 170 170 158 133 114 140
1951 145 150 178 163 172 178 199 199 184 162 146 166
1952 171 180 193 181 183 218 230 242 209 191 172 194
1953 196 196 236 235 229 243 264 272 237 211 180 201
1954 204 188 235 227 234 264 302 293 259 229 203 229
1955 242 233 267 269 270 315 364 347 312 274 237 278
1956 284 277 317 313 318 374 413 405 355 306 271 306
1957 315 301 356 348 355 422 465 467 404 347 305 336
1958 340 318 362 348 363 435 491 505 404 359 310 337
1959 360 342 406 396 420 472 548 559 463 407 362 405
1960 417 391 419 461 472 535 622 606 508 461 390 432

2. Base R: The Simple and Straightforward Route

Base R offers a direct path to creating a time series plot:

plot(AirPassengers)

This generates a basic line plot, revealing an upward trend in air passengers over time.

3. ggplot2: The High-Flying, Visually Staggering Journey

For more customization and visual appeal, we’ll turn to the ggplot2 library and the healthyR.ts library to first convert the AirPassengers Data set into a tibble:

library(ggplot2)
library(dplyr)
library(healthyR.ts)

df <- ts_to_tbl(AirPassengers)

ggplot(df, aes(x = date_col, y = value)) +
  geom_line() +
  theme_minimal() +
  labs(title = "Monthly Air Passengers (1949-1960)",
       x = "Year",
       y = "Passengers")

This creates a more refined plot with informative labels and a sleeker aesthetic.

4. Mastering Time with scale_x_date()

To fine-tune the x-axis date labels, ggplot2 offers the versatile scale_x_date() function. Let’s display years and abbreviated months:

ggplot(df, aes(x = date_col, y = value)) +
  geom_line() +
  theme_minimal() +
  scale_x_date(date_labels = "%b %Y") +
  labs(title = "Monthly Air Passengers (1949-1960)",
       y = "Passengers")

5. Your Turn to Pilot: Experiment and Explore!

R is your playground for time series visualization! Try these challenges:

  • Explore other time series datasets in R.
  • Customize plots further with colors, themes, and annotations.
  • Use scale_x_date() to display different date formats.
  • Combine multiple time series in a single plot.

Unleash your creativity and uncover the captivating stories hidden within time series data! For a start here are some resources:

The scale_x_date() functiontakes the following arguments:

  • %d: Day as a number between 0 and 31
  • %a: Abbreviated weekday (e.g. “Tue”)
  • %A: Unabbreviated weekday (e.g. “Tuesday”)
  • %m: Month between 0 and 12
  • %b: Abbreviated month (e.g. “Jan”)
  • %B: Unabbreviated month (e.g. “January”)
  • %y: 2-digit year (e.g. “21”)
  • %Y: 4-digit year (e.g. “2021”)
  • %W: Week of the year between 0 and 52
To leave a comment for the author, please follow the link and comment on their blog: Steve's Data Tips and Tricks.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)