Skip to content

ggplot2: Two Time Series With Different Dates

May 5, 2009

In his blogpost Jon Peltier shows how to “make a line chart which has two time series, in which each time series has data for different dates”, both in Excel 2003 and Excel 2007. The process involves a number of steps, some of which are not very intuitive.

Creating the same chart in ggplot2 is very easy.

> library(ggplot2)
> df <- structure(list(date = structure(c(4L,
     10L, 16L, 19L, 2L, 14L, 20L, 6L, 12L, 4L,
     5L, 10L, 11L, 13L, 15L, 16L, 17L, 18L,
     1L, 2L, 3L, 7L, 8L, 9L, 14L, 20L), .Label = c("01-Dec-08",
     "02-Dec-08", "04-Dec-08", "04-Nov-08",
     "05-Nov-08", "06-Jan-09", "08-Dec-08",
     "10-Dec-08", "11-Dec-08", "11-Nov-08",
     "12-Nov-08", "13-Jan-09", "13-Nov-08",
     "16-Dec-08", "17-Nov-08", "18-Nov-08",
     "20-Nov-08", "24-Nov-08", "25-Nov-08",
     "30-Dec-08"), class = "factor"), value = c(5.9,
     6, 6.1, 6.2, 6.1, 5.8, 6.1, 5.5, 5.9, 5.7,
     6.2, 6.1, 5.7, 6, 6, 6.2, 6.2, 6.1, 6,
     5.7, 6.1, 5.9, 5.7, 6, 5.9, 6.1), variable = structure(c(1L,
     1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
     2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
     2L, 2L, 2L, 2L, 2L), .Label = c("A", "B"),
     class = "factor")), .Names = c("date",
     "value", "variable"), class = "data.frame",
     row.names = c(NA, -26L))
> df$date <- as.Date(df$date, format = "%d-%b-%y")
> ggplot(df, aes(x = date, y = value, group = variable,
     colour = variable)) + geom_point() + geom_line()

Now let’s apply a more minimalist formatting and remove axes labels.

> last_plot() + theme_bw() + opts(panel.grid.major = theme_blank()) +
     xlab(NULL) + ylab(NULL)
9 Comments leave one →
  1. May 5, 2009 9:45 pm

    Thanks for this. Maybe you’d like to explain why we need the complicated structure(liste(date etc. for the data?


    • learnr permalink
      May 6, 2009 1:21 am

      I included the data in this format so that it could be easily possible to use the data and reproduce the plot(s) in the post.

      In fact, I first imported the data using read.csv(), and then used dput() to write a text representation of the R dataframe.

    • ekorn permalink
      August 6, 2009 12:23 am

      A human-readable alternative is

      df <- read.table(header=TRUE, textConnection("
      date value variable
      1 04-Nov-08 5.9 A
      2 11-Nov-08 6.0 A
      25 16-Dec-08 5.9 B
      26 30-Dec-08 6.1 B

      Spread the word! 😎

  2. Pastor permalink
    January 12, 2010 2:25 am

    Hi, thanks , nice plot! I m trying to applied qplot on two time series data like :

    02.04.2008 09:00;6.749,24;6.755,55;6.746,89;6.754,11;0
    02.04.2008 09:01;6.754,70;6.754,70;6.748,13;6.749,55;0
    02.04.2008 09:02;6.749,36;6.757,00;6.745,50;6.749,38;0

    unfortunatly the plot dose not work, any idea how to make it work?
    Thanks in advance for advice


    • learnr permalink*
      January 12, 2010 12:04 pm

      If you specified what type of plot you are trying to create, it would be easier to help you!

      • pastor permalink
        January 12, 2010 9:43 pm

        I like to plot two plot of the two financial time series to compare both. As x- axis would be the date (scala month) and the y-axis the values of the both series as it is schowned in the plot on this site.

        I hope you have any idea…. I did not figuerd out how to make it…Pastor

      • learnr permalink*
        January 14, 2010 8:21 pm

        First you have to import the data into R dataframe. Then you can try something like this:
        ggplot(df, aes(DATE)) + geom_line(aes(y=HIGH)) + geom_line(aes(y=LOW))

  3. pastor permalink
    January 23, 2010 1:52 am

    Hi, thnaks for your code.
    I used did the following:
    ggplot(mydata, aes(DATE)) + geom_line(aes(y=HIGH)) + geom_line(aes(y=LOW))

    then got the error: (any idea?)

    Fehler in paste(rep(l, length(lvs)), rep(lvs, each = length(l)), sep = sep) :
    Vektoren negativer Länge sind nicht erlaubt
    Zusätzlich: Warnmeldung:
    In ans * length(l) : NAs durch Ganzzahlüberlauf erzeugt

  4. May 5, 2016 1:00 am

    Thank you for this post. After struggling the whole afternoon trying to plot some data similar to yours, this solved my problem.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: