[R] how to add legend to time series plot

michael.wolf@upf.edu michael.wolf at upf.edu
Wed Apr 14 15:24:01 CEST 2004

Dear all,

I would like to add a legend to a time series plot, but 
cannot get it done. I have searched the archive about this,
but to no avail ...

I have three sets of time series data stored in a matrix
wMat. The following code plots the the data with
a legend, but it does not put the time on the x-axis:

matplot(y = wMat, type = "l",
        ylab = "Allocation", main = "GARCH")
legend(1, -0.3, c("Stocks", "Bonds", "Cash"), col = 1:3, lty = 1:3)

The following code puts the time on the x-axis but now the
legend does not show up:

wMat = ts(wMat, frequency = 12, start = c(1968, 2))
ts.plot(wMat[,1], wMat[,2], wMat[,3], col = 1:3, lty = 1:3,
        ylab = "Allocation", main = "GARCH")
legend(1, -0.3, c("Stocks", "Bonds", "Cash"), col = 1:3, lty = 1:3)

Can anybody help with this? If needed, I can send along postscript files
of the resulting plots.

I do not subscribe to the list, so please (also) reply directly to me.

Thanks very much,

More information about the R-help mailing list