Enrico Dace
Enrico Dace

Reputation: 115

conversion Time_Series to data.frame

I have the following structure:

  'data.frame': 240 obs. of  3 variables:
 $ Date  : Date, format: "1998-01-01" "1998-02-01" "1998-03-01" ...
 $ rollDE: Time-Series  from 1 to 240: 0.00289 0.00992 -0.00424 0.07958 -0.035 ...
 $ DE$DE : num  0.00289 0.00992 -0.00424 0.07958 -0.035 ...

I have implemented the following code for a ggplot of a multiple columns in one plot.

> ggplot() + 
+   geom_line(data = yy, aes(x= Date, y = x$`rollDE`, color = "rollingmeanDE")) +
+   geom_line(data = yy, aes(x = Date, y = x$`DE$DE`, color = "DEreturn")) +
+   xlab('Date') + ylab('percent.change')

However, error as follows.

Don't know how to automatically pick scale for object of type ts. Defaulting to continuous.

I have done attempts to convert $rollDE but failed. Please any comment to convert it or any other solution?

Upvotes: 1

Views: 102

Answers (1)

G. Grothendieck
G. Grothendieck

Reputation: 269905

Convert to zoo using read.zoo and use autoplot.zoo. Omit facet = NULL to get separate panes for the two data columns. Using yy in the Note at the end:

library(ggplot2)
library(zoo)

autoplot(read.zoo(yy), facet = NULL) + xlab("Date") + ylab("percentage change")

screenshot

Note

Here is a reproducible version of yy. The last column has been doubled so that it does not have identical values to the column before it.

yy <- data.frame(
  Date = as.Date(c("1998-01-01", "1998-02-01", "1998-03-01")),
  rollDE = ts(c(0.00289, 0.00992, -0.00424)),
  "DE$DE" = 2*c(0.00289, 0.00992, -0.00424))

Upvotes: 1

Related Questions