Below is what my data looks like. My data is called sales1156
.
以下是我的数据。我的数据叫做sales1156。
> sales1156
date.and.time hsales
06/01/11 09:00 14.00
06/01/11 10:00 28.00
06/01/11 11:00 28.00
06/01/11 12:00 28.00
06/01/11 13:00 28.00
06/01/11 14:00 28.00
The data continues till 4th Oct 2013(04/10/2013). I have used the following commands to create a time series object.
数据一直持续到2013年10月4日(2013年10月4日)。我使用以下命令创建时间序列对象。
> hsales1156xts<-as.xts(sales1156,order.by=as.Date(sales1156$date.and.time,frequency=24))
> is.xts(hsales1156xts)
[1] TRUE
The problem is that I am not able to plot a proper graph.
问题是我无法绘制正确的图表。
> plot.xts(hsales1156xts) # This command is throwing a warning as mentioned below
Warning message:
In plot.xts(hsales1156xts) : only the univariate series will be plotted
* is not allowing me to attach the graph. Someone please help me to plot this time series. Any good read or suggestion would be great. I am unable to make much out of the xts and zoo documents. Thus a little detailed syntax and explanation is required.
*不允许我附加图表。有人请帮我画这个时间序列。任何好的阅读或建议都会很棒。我无法从xts和zoo文档中做出太多贡献。因此需要一些详细的语法和解释。
1 个解决方案
#1
1
The date column needs to be excluded from data input in as.xts(x=
需要从as.xts中的数据输入中排除日期列(x =
Test Example:
测试示例:
require(PerformanceAnalytics)
data(economics)
colnames(economics)
#[1] "date" "pce" "pop" "psavert" "uempmed" "unemploy"
#Subset your timeseries
economics_sub=economics[,c("date","uempmed")]
#Ensure your date or datetime object is in the correct format
economics_sub$date=as.Date(economics_sub[,1],format="%Y-%m-%d")
#Exclude date column whie reading data in "x ="
economics_xts<-as.xts(x=economics_sub[,"uempmed"],order.by=economics_sub[,"date"])
colnames(economics_xts)=colnames(economics_sub)[-1]
head(economics_xts)
# uempmed
#1967-06-30 4.5
#1967-07-31 4.7
#1967-08-31 4.6
#1967-09-30 4.9
#1967-10-31 4.7
#1967-11-30 4.8
#Plot Series using PerformanceAnalytics function 'chart_Series'
chart_Series(economics_xts)
Your Example:
你的例子:
#Data input
sales1156=read.csv(text='date.time,hsales
"06/01/11 09:00",14.00
"06/01/11 10:00",28.00
"06/01/11 11:00",28.00
"06/01/11 12:00",28.00
"06/01/11 13:00",28.00
"06/01/11 14:00",28.00',header=TRUE)
#Check format of your datetime index
str(sales1156)
#'data.frame': 6 obs. of 2 variables:
# $ date.time: Factor w/ 6 levels " 06/01/11 09:00",..: 1 2 3 4 5 6
# $ hsales : num 14 28 28 28 28 28
#The datetime index has been read as a factor and not as datetime object
#Convert datetime to appropriate format, in this case POSIXct format
sales1156$date.time=as.POSIXct(sales1156$date.time,format="%d/%m/%y %H:%M")
#Check if your formatting has worked as intended
str(sales1156)
#'data.frame': 6 obs. of 2 variables:
# $ date.time: POSIXct, format: "2011-01-06 09:00:00" "2011-01-06 10:00:00" ...
# $ hsales : num 14 28 28 28 28 28
#Converion to xts,exclude date column whie reading data in "x ="
hsales1156xts<-as.xts(x=sales1156[,"hsales"],order.by=sales1156[,"date.time"])
colnames(hsales1156xts)=colnames(sales1156)[-1]
head(hsales1156xts)
# hsales
#2011-01-06 09:00:00 14
#2011-01-06 10:00:00 28
#2011-01-06 11:00:00 28
#2011-01-06 12:00:00 28
#2011-01-06 13:00:00 28
#2011-01-06 14:00:00 28
#Plot Series using PerformanceAnalytics function 'chart_Series'
chart_Series(hsales1156xts)
#1
1
The date column needs to be excluded from data input in as.xts(x=
需要从as.xts中的数据输入中排除日期列(x =
Test Example:
测试示例:
require(PerformanceAnalytics)
data(economics)
colnames(economics)
#[1] "date" "pce" "pop" "psavert" "uempmed" "unemploy"
#Subset your timeseries
economics_sub=economics[,c("date","uempmed")]
#Ensure your date or datetime object is in the correct format
economics_sub$date=as.Date(economics_sub[,1],format="%Y-%m-%d")
#Exclude date column whie reading data in "x ="
economics_xts<-as.xts(x=economics_sub[,"uempmed"],order.by=economics_sub[,"date"])
colnames(economics_xts)=colnames(economics_sub)[-1]
head(economics_xts)
# uempmed
#1967-06-30 4.5
#1967-07-31 4.7
#1967-08-31 4.6
#1967-09-30 4.9
#1967-10-31 4.7
#1967-11-30 4.8
#Plot Series using PerformanceAnalytics function 'chart_Series'
chart_Series(economics_xts)
Your Example:
你的例子:
#Data input
sales1156=read.csv(text='date.time,hsales
"06/01/11 09:00",14.00
"06/01/11 10:00",28.00
"06/01/11 11:00",28.00
"06/01/11 12:00",28.00
"06/01/11 13:00",28.00
"06/01/11 14:00",28.00',header=TRUE)
#Check format of your datetime index
str(sales1156)
#'data.frame': 6 obs. of 2 variables:
# $ date.time: Factor w/ 6 levels " 06/01/11 09:00",..: 1 2 3 4 5 6
# $ hsales : num 14 28 28 28 28 28
#The datetime index has been read as a factor and not as datetime object
#Convert datetime to appropriate format, in this case POSIXct format
sales1156$date.time=as.POSIXct(sales1156$date.time,format="%d/%m/%y %H:%M")
#Check if your formatting has worked as intended
str(sales1156)
#'data.frame': 6 obs. of 2 variables:
# $ date.time: POSIXct, format: "2011-01-06 09:00:00" "2011-01-06 10:00:00" ...
# $ hsales : num 14 28 28 28 28 28
#Converion to xts,exclude date column whie reading data in "x ="
hsales1156xts<-as.xts(x=sales1156[,"hsales"],order.by=sales1156[,"date.time"])
colnames(hsales1156xts)=colnames(sales1156)[-1]
head(hsales1156xts)
# hsales
#2011-01-06 09:00:00 14
#2011-01-06 10:00:00 28
#2011-01-06 11:00:00 28
#2011-01-06 12:00:00 28
#2011-01-06 13:00:00 28
#2011-01-06 14:00:00 28
#Plot Series using PerformanceAnalytics function 'chart_Series'
chart_Series(hsales1156xts)