简体   繁体   中英

Plotting in R with Dates

             alpha(1%)    alpha(5%)      realized
2010-12-21 -0.01509998 -0.009001583 -2.035892e-03
2010-12-22 -0.01440173 -0.008577771  9.735154e-04
2010-12-23 -0.01389563 -0.008287957  1.858982e-03
2010-12-27 -0.01376369 -0.008223168 -1.597613e-03
2010-12-28 -0.01337600 -0.007983994  2.548457e-04
2010-12-29 -0.01306351 -0.007802134 -2.021994e-03

Hi, Above is the data I am working with. When I try to plot the forecast using the code

plot(modelroll@forecast$VaR[,"alpha(1%)"])

where modelroll@forecast$VaR is the above data, I just get number of observations on the x axis whereas I need dates. Why is R not recognizing the dates. When I plot the returns series, it does recognize the dates on x axis. Also One more question how do I extract particular rows with a column for eg alpha(1%) column for first 10 observations with using a loop. I tried

modelroll@forecast$VaR[1:10,"alpha(1%)"]

but it doesn't work. Thanks in advance.

dput(head(modelroll@forecast$VaR))
structure(list(`alpha(1%)` = c(-0.0150999800078312, -0.0144017267128711, 
-0.0138956320928591, -0.0137636901290749, -0.0133759968008841, 
-0.0130635141114229), `alpha(5%)` = c(-0.00900158285193088, -0.0085777709430231, 
-0.0082879574620513, -0.00822316834000325, -0.00798399426075919, 
-0.0078021335073256), realized = c(-0.0020358922408686, 0.000973515436500283, 
0.00185898240838507, -0.00159761300810324, 0.000254845717341201, 
-0.00202199418958748)), .Names = c("alpha(1%)", "alpha(5%)", 
"realized"), row.names = c("2010-12-21", "2010-12-22", "2010-12-23", 
"2010-12-27", "2010-12-28", "2010-12-29"), class = "data.frame")

Suppose if df is the dataset,

m1 <- as.matrix(df)
d <- as.Date(row.names(m1))
matplot(d, m1, pch=1, xaxt='n')
axis(1, at=d, labels=d)
legend(x='topright', y=3.5, c(' alpha(1%)', ' alpha(5%)', '  realized'),
   pch='15r', col=2:4)

Also,

 m1[1:3, 'alpha(1%)']
 #2010-12-21  2010-12-22  2010-12-23 
 #-0.01509998 -0.01440173 -0.01389563 

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM