views:

110

answers:

3

Hi all,

A very newbish question, but say I have data like this:

test_data <- data.frame(
var0 = 100 + c(0, cumsum(runif(49, -20, 20))),
var1 = 150 + c(0, cumsum(runif(49, -10, 10))),
date = seq.Date(as.Date("2002-01-01"), by="1 month", length.out=100))

How can I plot both time series var0 and var1 on the same graph, with date on the x-axis, using ggplot2? Bonus points if you make var0 and var1 different colours, and can include a legend!

I'm sure this is very simple, but I can't find any examples out there.

+1  A: 

Using your data:

test_data <- data.frame(
var0 = 100 + c(0, cumsum(runif(49, -20, 20))),
var1 = 150 + c(0, cumsum(runif(49, -10, 10))),
Dates = seq.Date(as.Date("2002-01-01"), by="1 month", length.out=100))

I create a stacked version which is what ggplot() would like to work with:

stacked <- with(test_data,
                data.frame(value = c(var0, var1),
                           variable = factor(rep(c("Var0","Var1"),
                                                 each = NROW(test_data))),
                           Dates = rep(Dates, 2)))

In this case producing stacked was quite easy as we only had to do a couple of manipulations, but reshape() and the reshape and reshape2 might be useful if you have a more complex real data set to manipulate.

Once the data are in this stacked form, it only requires a simple ggplot() call to produce the plot you wanted with all the extras (one reason why higher-level plotting packages like lattice and ggplot2 are so useful):

require(ggplot2)
p <- ggplot(stacked, aes(Dates, value, colour = variable))
p + geom_line()

I'll leave it to you to tidy up the axis labels, legend title etc.

HTH

Gavin Simpson
@ucfagls - I think you have a misplaced parens in your code up there. I think this is what you are after: stacked <- with(test_data, data.frame(value = c(var0, var1), variable = factor(rep(c("Var0", "Var1"))), each = NROW(test_data), Dates = rep(date, 2))). Also, what is the purpose of the column "each"? And is this not just a more convoluted and less efficient way to melt the data as shown by rcs? I guess I could imagine an instance where melt wouldn't get the job done, but it is almost certainly the right tool for this job unless I'm missing something?
Chase
@chase, sorry, that is Emacs ESS getting the indenting wrong. each is an argument to `rep()`, so we really are only getting 3 cols in `stacked`. I'll edit the code to make the indent clearer.
Gavin Simpson
@chase; your comment about `melt()` is well taken, and I note that the reshape[2] package would be useful here. I'm not that familiar with reshape2 and for such a simple manipulation doing it by hand is more complex than a call to `melt()`, it was less effort as I didn't need to read how to use `melt()`. And rcs sneaked in with his answer whilst I was producing mine; when I started the reply there had been no answers. more than one way to skin a cat - as they say! ;-)
Gavin Simpson
+3  A: 

Try this

require("reshape")
require("ggplot2")

test_data_long <- melt(test_data, id="date")  # convert to long format

ggplot(data=test_data_long,
       aes(x=date, y=value, colour=variable)) +
    geom_line()

ggplot2 output

rcs
+2  A: 

For a small number of variables, you can use build up the plot manually yourself:

ggplot(test_data, aes(date)) + 
  geom_line(aes(y = var0, colour = "var0")) + 
  geom_line(aes(y = var1, colour = "var1"))
hadley