3

all.

I read several previous message at stackoverflow, and went through the documentation of zoo and ggplot2 but didn't find any suitable answer.

Say I have a zoo object called 'data'. The original data in the flat file are as follows:

Date,Quote1,Quote2,Quote3,Quote4,Quote5
18/07/2008,42.36,44.53,28.4302,44.3,42
21/07/2008,43.14,44.87,28.6186,44.83,43.27
22/07/2008,43.26,44.85,28.6056,44.86,42.84
23/07/2008,44.74,45.61,29.7558,45.69,#N/A
24/07/2008,43.99,45.14,29.2944,45.19,#N/A
25/07/2008,43.18,45.33,29.4569,45.46,43.65
28/07/2008,43.45,44.72,28.5016,44.89,43.31
29/07/2008,43.49,44.8,28.1247,44.88,42.85
30/07/2008,44.55,45.54,28.0727,45.58,43.67
31/07/2008,43.36,45.5,27.9818,45.63,43.91
01/08/2008,43.34,44.75,28.0792,44.69,43.04

Now, I want to plot the time series of this five financial products on a single line graph so that to compare their evolution.

I wish to use the ggplot2.

Would anyone be kind to give me some hints?

tagoma
  • 3,896
  • 4
  • 38
  • 57
  • Have a look at melt (from reshape) to prepare your data, and then plotting using ggplot2 should be straightforward. Look at http://stackoverflow.com/questions/9531904/plot-multiple-columns-on-the-same-graph-in-r – Chargaff Dec 12 '12 at 20:49

3 Answers3

9

If data is your zoo object then try this (and see ?autoplot.zoo for more info):

p <- autoplot(data, facet = NULL)
p

or perhaps this since I don't think the automatic varying of linetype looks so good with this many series in the same panel:

p + aes(linetype = NULL)

enter image description here

G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
  • that works great. thanks. now, i want to date format : mm/yyyy . i then added + scale_x_date(format = "%m/%Y") , but that doesn't work. i mean the dates remain the format they are in the original data source – tagoma Dec 13 '12 at 21:01
  • 1
    Try this: `library(scales); p + scale_x_date(labels = date_format("%m/%Y"))` – G. Grothendieck Dec 13 '12 at 21:08
  • 1
    ah thanks. and my scale package was not loaded. and do the ylab and xlab functions works with ggplot2 graphs? – tagoma Dec 13 '12 at 21:18
  • 1
    ah, i did as follows: autoplot(p, facet = NULL, main = "Cumulative Daily Returns", xlab = NA, ylab = "%") + scale_x_date(labels = date_format("%Y"), xlab("")) + scale_y_continuous(ylab("%")) . i don't want any name for the x-axis, i then put "", but there may be better ways – tagoma Dec 13 '12 at 21:32
4

Here is one way to do it:

df <- read.csv(text = "Date,Quote1,Quote2,Quote3,Quote4,Quote5
18/07/2008,42.36,44.53,28.4302,44.3,42
21/07/2008,43.14,44.87,28.6186,44.83,43.27
22/07/2008,43.26,44.85,28.6056,44.86,42.84
23/07/2008,44.74,45.61,29.7558,45.69,#N/A
24/07/2008,43.99,45.14,29.2944,45.19,#N/A
25/07/2008,43.18,45.33,29.4569,45.46,43.65
28/07/2008,43.45,44.72,28.5016,44.89,43.31
29/07/2008,43.49,44.8,28.1247,44.88,42.85
30/07/2008,44.55,45.54,28.0727,45.58,43.67
31/07/2008,43.36,45.5,27.9818,45.63,43.91
01/08/2008,43.34,44.75,28.0792,44.69,43.04", na.string = "#N/A")

df$Date <- strptime(df$Date, format = "%d/%m/%Y")

Create a zoo object:

library(zoo)
dat <- zoo(df[-1], df$Date)

Transform the object to a data frame for ggplot2:

df_new <- data.frame(value = as.vector(dat),
                     time = time(dat),
                     quote = rep(names(dat), each = nrow(dat)))

Plot:

library(ggplot2)
ggplot(df_new, aes(y = value, x = time, colour = quote)) + geom_line()

enter image description here

Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168
1

Here's another slightly different method, using melt from reshape

# Read your data and format date (as proposed by Sven)

df <- read.csv(text = "Date,Quote1,Quote2,Quote3,Quote4,Quote5
18/07/2008,42.36,44.53,28.4302,44.3,42
21/07/2008,43.14,44.87,28.6186,44.83,43.27
22/07/2008,43.26,44.85,28.6056,44.86,42.84
23/07/2008,44.74,45.61,29.7558,45.69,#N/A
24/07/2008,43.99,45.14,29.2944,45.19,#N/A
25/07/2008,43.18,45.33,29.4569,45.46,43.65
28/07/2008,43.45,44.72,28.5016,44.89,43.31
29/07/2008,43.49,44.8,28.1247,44.88,42.85
30/07/2008,44.55,45.54,28.0727,45.58,43.67
31/07/2008,43.36,45.5,27.9818,45.63,43.91
01/08/2008,43.34,44.75,28.0792,44.69,43.04", na.string = "#N/A")

df$Date <- strptime(df$Date, format = "%d/%m/%Y")

library(reshape)
# reshape your data with melt
melted <- melt(df[-1])
# add dates
melted2 <- cbind(df$Date,melted)
# plot with ggplot
ggplot(melted2,aes(y = value, x = melted2[,1], color = variable)) + geom_line()

plot

Chargaff
  • 1,562
  • 2
  • 19
  • 41