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)
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()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.