I have a data frame containing multiple time series of returns, stored in columns.
The first column contains dates, and subsequent columns are independent time series each with a name. The column headers are the variable names.
## I have a data frame like this
t <- seq(as.Date('2009-01-01'),by='days',length=10)
X <- rnorm(10,0,1)
Y <- rnorm(10,0,2)
Z <- rnorm(10,0,4)
dat <- data.frame(t,X,Y,Z)
## which appears as
t X Y Z
1 2009-01-01 -1.8763317 -0.1885183 -6.655663
2 2009-01-02 -1.3566227 -2.1851226 -3.863576
3 2009-01-03 -1.3447188 2.4180249 -1.543931
I want to plot each time series as a line on a separate plot, in a lattice, with each plot labeled by the variable names.
To plot this with lattice, the data must be in a tall format, as such:
t symbol price
1 2009-01-01 X -1.8763317
2 2009-01-02 Y -0.1885183
2 2009-01-02 Z -6.655663
What is a good function call to do this?
you can also use melt() from the 'reshape' library (I think it's easier to use than reshape() itself) - that'll save you the extra step of having to add the time column back in...
> library(reshape)
> m <- melt(dat,id="t",variable_name="symbol")
> names(m) <- sub("value","price",names(m))
> head(m)
t symbol price
1 2009-01-01 X -1.14945096
2 2009-01-02 X -0.07619870
3 2009-01-03 X 0.01547395
4 2009-01-04 X -0.31493143
5 2009-01-05 X 1.26985167
6 2009-01-06 X 1.31492397
> class(m$t)
[1] "Date"
> library(lattice)
> xyplot( price ~ t | symbol, data=m ,type ="l", layout = c(1,3) )
For this particular task, however, I would consider using the 'zoo' library, which would not require you to reshape the data frame:
> library(zoo)
> zobj <- zoo(dat[,-1],dat[,1])
> plot(zobj,col=rainbow(ncol(zobj)))
R developers/contributors (Gabor and Hadley in this case) have blessed us with many great choices. (and can't forget Deepayan for the lattice package)
From tidyr gather help page:
Examples
library(tidyr)
library(dplyr)
# From http://stackoverflow.com/questions/1181060
stocks <- data.frame(
time = as.Date('2009-01-01') + 0:9,
X = rnorm(10, 0, 1),
Y = rnorm(10, 0, 2),
Z = rnorm(10, 0, 4)
)
gather(stocks, stock, price, -time)
stocks %>% gather(stock, price, -time)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With