I would like to create d plots including "dailydata", "MAw" and "average":
where "dailydata" are
dailydata <- structure(list(CREDIT.AGRICOLE = c(0.0129, 0.0132, 0.0136, 0.0125, 0.0118,
0.0114, 0.0114, 0.0106, 0.0107, 0.0116, 0.0122, 0.0126, 0.0137, 0.0141,0.0137),
SOCIETE.GENERAL = c(0.0178, 0.0182, 0.0187, 0.0175, 0.0167, 0.0165, 0.0160, 0.0155, 0.0155,
0.0162, 0.0182, 0.0195, 0.0205, 0.0207, 0.0201)),
.Names = c("CREDIT.AGRICOLE", "SOCIETE.GENERAL"),
row.names = c("18/11/2008", "19/11/2008", "20/11/2008", "21/11/2008", "22/11/2008",
"23/11/2008", "24/11/2008", "25/11/2008", "26/11/2008", "27/11/2008", "28/11/2008",
"29/11/2008", "30/11/2008", "01/12/2008", "02/12/2008"), class = "data.frame")
and "MAw" and "average" I obtain like this
w <- 3 #MA(w)
n <- length(dailydata[,1])+0 #length of data
d <- length(dailydata[1,])+0 #nr of obligors
#extract row and col names
obligorname <- colnames(dailydata)
dates <- rownames(dailydata)
# calculate moving average ma(w) from daily data
#--------------------------------------------
MAw <- vector()
for(j in seq(from=1, to=d, 1)) #1,2
{
vec_MAw <- vector()
for (i in seq(from=1, to=n-w+1, 1)) #1,2,..., 13
{
mean <- sum(dailydata[i:(w-1+i),j])/w
vec_MAw <- rbind(vec_MAw, mean)
}
MAw <- cbind( MAw, vec_MAw)
}
#name rows and cols
colnames(MAw) <- obligorname
rownames(MAw) <- dates[w:n]
#calculate average
#-----------------
average <- vector()
for(j in seq(from=1, to=d, 1)) # 1, 2
{
obligor_average <- sum(dailydata[1:n,j])/n
average <- cbind(average, obligor_average)
}
#name rows and cols
colnames(average) <- obligorname
rownames(average) <- dates[n]
I would like to have d plots (one for each obligor, in this example d=2).
Firstly, on x axis, each of these plots should have only few dates which are perpendicular to the x axis.
Secondly, in the figure,
the series from "dailydata" should start at start date (18/11/2008) and end at end date (02/12/2008)
the series from "MAw" should start at "w-th date" (here w=3, therefore 20/11/2008) and finish at end date (02/12/2008)
there should be a horizontal line for the average value
The only thing which I have is this,
for (i in seq(from=1, to=d, 1))
{
plot( dailydata[,i], main= obligorname[i], type = "l", col="red", las=1,
ylab="dailydata", xlab="time", ylim = c(0, max(dailydata[i])))
abline(h=average[,i], col="black")
lines(MAw[,i], col="blue")
}
which does not give the figure I would like to obtain since the MAw serie should be shifted to the right and there are no dates on the x axis.
I am new to SO and to R and maybe this is not the minimum amount of info necessary to describe my problem. Anyway, what do you suggest?
Should I use as.Date for dates and plot.ts? Or should I use ggplot2? Or something else? And how?
Thank you a lot!