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: However, the plot is not how it is supposed to be. Could you help me understand why?
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)
Otherwise, using ts
:
tsbc <- ts(bc$Price, start=c(2016,1), frequency=365)
autoplot(tsbc) + scale_x_yearmon(n=5)