3

I have a dataset "bc" with 2285 observations two variables: "Date" and "Price".

 'data.frame':  2285 obs. of  2 variables:
  $ Date : Date, format: "2017-12-14" "2017-12-13" ...
  $ Price: num  16234 16250 16650 16470 14691 ...

I tried to create a time series object as:

  tsbc <- ts(bc)

Then, I used:

  autoplot(tsbc)

And I get figure below: Plot-Image However, the plot is not how it is supposed to be. Could you help me understand why?

Hunter Jiang
  • 1,300
  • 3
  • 14
  • 23
Manuel
  • 33
  • 1
  • 6

2 Answers2

2

Here is a solution based on xts:

library(ggplot2)

# Generate a dataset
set.seed(1)
bc <- data.frame(Date=seq(as.Date("2016/1/1"), as.Date("2017/12/14"), "days"),
                 Price= cumsum(rnorm(714)))
#   'data.frame':   714 obs. of  2 variables:
# $ Date : Date, format: "2016-01-01" "2016-01-02" ...
# $ Price: num  -0.626 -0.443 -1.278 0.317 0.646 ...

library(xts)
tsbc <- xts(bc$Price, order.by=bc$Date)
autoplot(tsbc)

enter image description here

Otherwise, using ts:

tsbc <- ts(bc$Price, start=c(2016,1), frequency=365)
autoplot(tsbc) + scale_x_yearmon(n=5)

enter image description here

Marco Sandri
  • 23,289
  • 7
  • 54
  • 58
2

The problem is how you created that time series object tsbc. You are actually creating two time series. And since dates are just numbers under the hood with a class attribute, they lose their attribute when you call ts. That's why autoplot draws a line for Date as well which plots the doubles representing the respective date (see ?base::Dates for more details). Which is obviously not what you want. See @Marco's answer for how to construct the ts object.

However, you don't even need to do that. Why not simply

library(ggplot2)
ggplot(bc, aes(Date, Price)) + geom_line() 
markus
  • 25,843
  • 5
  • 39
  • 58