[R] legend/text in time series plot

Uwe Ligges ligges at statistik.uni-dortmund.de
Sun May 6 11:27:13 CEST 2001

Hiroyuki Kawakatsu wrote:
> hi,
> i need help on placing legend/text in a time series plot. here is what
> i am doing (i am using rw1022 on windoze 2000):
> #read data file
> gdpn <- scan("jngdpsa.dat", list(year=0, qtr=0, gdp=0));
> gdpr <- scan("jrgdpsa.dat", list(year=0, qtr=0, gdp=0));
> #convert to time series object
> gdpn <- ts(gdpn$gdp, frequency=4, start=c(1955,2));
> gdpr <- ts(gdpr$gdp, frequency=4, start=c(1955,2));
> #plot two gdp time series in one graph
> postscript("fig2-1j.eps", horizontal=FALSE, onefile=FALSE, height=5, width=6);
> plot(gdpn, col="red", xlab="", ylab="", type="l");
> text(75, 500, labels="Nominal", col="red");
> lines(gdpr, col=c("blue"));
> text(30, 1300, labels="Real", col="blue");
> dev.off();
> #end of code
> the problem, of course, is that the text() function is not putting any
> text in the plot. if i run this code *without* converting to time
> series objects, the texts are placed as intended. i first tried
> calling legend() on the time series plot and it also does not put any
> legends. so my question is how can i put text/legend in a time series
> plot? obviously there is a problem of how to specify the x coordinate
> in a time series plot since the horizontal axis is time. if i call
> par("usr"), it appears that the plotting coordinate is between 0 and
> 1. however, even if i specify the position as (0.5,0.5), the text
> still does not appear in the graph. any help is appreciated.

So x- AND y-coords are within par("usr")?

Just taking the first example from ?ts and adding "Hello World" or a
legend works fine:

  my.ts <- ts(1:10, frequency = 4, start = c(1959, 2)) 
# [1] 1959.16 1961.59    0.64   10.36
  text(1960, 5, "Hello World")
  legend(1960, 6, legend="My legend")

If you don't get the point, can you provide a reproduceable example (but
not a large dataset, please)?

Uwe Ligges
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list