[R] how to add legend to time series plot

Uwe Ligges ligges at statistik.uni-dortmund.de
Thu Apr 15 13:06:47 CEST 2004


michael.wolf at upf.edu wrote:

> 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)

I guess it is clipped in this case. Use par("usr") after ts.plot() to 
see what the user coordinates are, and whether (1, -0.3) is a sensible 
location to place the legend.

Uwe Ligges


> 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,
> Michael
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html




More information about the R-help mailing list