2013-06-04 53 views
0

我使用的sp500ret數據從rugarch包:如何在情節中將rownames繪製爲日期?

library(rugarch) 
data(sp500ret) 

這(至少我想是這樣)一個數據幀,其給出的回報率。 rownames是日期。現在,我要繪製的回報和日期,所以

plot(dates,sp500ret) 

當然的,但是,這是行不通的,因爲日期沒有指定,但我怎麼能使用rownames爲相應的日期?

注:我不知道這件事,但我讀了一些關於xts文件的內容。這是一個xts文件,我怎麼能在這種情況下繪製它?

編輯: 我想過這樣的事情,但它不工作:

plot(as.Date(rownames(sp500ret), "%Y%m%d"),sp500ret) 

2日編輯: 也plot(rownames(sp500ret),sp500ret[,1])不起作用。

回答

2

轉換的rownames到日期如下:

library(rugarch) 

data(sp500ret) 

my.dates <- strptime(rownames(sp500ret), format="%Y-%m-%d") 

plot(my.dates,sp500ret$SP500RET, xlab="Date", las=1, col="steelblue", pch=20) 

# Or faster 

plot(as.Date(rownames(sp500ret),"%Y-%m-%d"),sp500ret[,1]) 

Date plot

# To specify custom x-axis 

par(mar=c(7, 4, 4, 2) + 0.1, bg="white", cex=1.5) # extend margin 

plot(my.dates,sp500ret$SP500RET, xlab="", las=1, col="steelblue", pch=20, xaxt="n") 

axis.Date(1, at=seq(my.dates[1], my.dates[length(my.dates)], "years"), 
      labels=seq(my.dates[1], my.dates[length(my.dates)], "years"), 
      format= "%Y-%m-%d", las=2) 

Date plot with custom x-axis

+0

我現在也發現:圖(as.Date(rownames(sp500ret),「%Y - %m-%d「),sp500ret [,1])這似乎也起作用了? –

+0

@StatTistician是的,這也是訣竅。 – COOLSerdash

+0

las = 1做什麼? –