[R] troubles with displaying legend on the plot

Jerome Asselin jerome at hivnet.ubc.ca
Tue Apr 15 19:47:07 CEST 2003


You have set par(usr=c(1979,2002,20,90)). The y-coordinate of your legend 
is outside this specified plotting range.

Hence, try legend(1979,50,[other options]) instead of
legend(1979,0,[other options]).

Jerome

On April 15, 2003 10:20 am, Wladimir Eremeev wrote:
> Dear colleagues,
>
> I have troubles while trying to display legend on the plot.
>
> I have data.frame fr
>
> > fr
>
>    year  M1             M2
> 1  1979  58.85198     56.77303
> 2  1980  57.59725     55.93749
> 3  1981  57.32133     55.55232
> 4  1982  54.69320     53.10566
> 5  1983  56.58973     55.03811
> 6  1984  58.81363     56.97641
> 7  1985  58.35583     56.82091
> 8  1986  60.41842     58.45457
> 9  1987  58.75928     57.03679
> 10 1988  59.89553     58.69077
> 11 1989  51.03595     49.45001
> 12 1990  52.23915     50.96713
> 13 1991  46.50311     45.50370
> 14 1992  47.38811     46.31649
> 15 1993  48.58066     47.44610
> 16 1994  46.04452     44.82441
> 17 1995  46.41809     45.54565
> 18 1996  37.65783     36.48118
> 19 1997  60.16313     58.63771
> 20 1998  51.47550     50.43720
> 21 1999  52.56228     51.64243
> 22 2000  41.71746     40.79734
> 23 2001  44.71175     44.05241
> 24 2002  49.78033     47.91608
>
> actually I've loaded it from MySQL database.
>
> Now drawing this plot with the following plot.r
>
> ===8<====
> op<-par(no.readonly=TRUE);
>
> par(usr=c(1979,2002,20,90),mar=c(7, 4, 4, 2) + 0.1);
>
> plot(fr$year,fr$M1
>      ,cex=0.8
>      ,type="o",ylim=c(20,90),xlim=c(1979,2002)
>      ,xlab="year",ylab="M, %"
>      ,pch=20
>      ,xaxt="n",
>      );
>
> lines(fr$year,fr$M2);
> points(fr$year,fr$M2,pch=21)
> axis(1,at=c(1979:2002));
>
> for(x in 1979:2002) {
>   abline(v=x, col = "lightgray", lty = "dotted");
> }
> for(y in seq(20,90,10)){
>   abline(h=y, col = "lightgray", lty = "dotted");
> }
> legend(1979,0,
>        legend=c("M1","M2"),
>        pch=c(20,21));
>
> par(op);
> ===8<===
>
> Legend doesn't appear.
> I tried different coordinates, (1979,0), (0,0),
> different colors, etc...
>
> Nothing helps. What should I do to make the legend to appear?
>
> I'm using R 1.6.2 for windows.
> OS: Windows NT workstation 4.0.
>
> Thank you in advance.



More information about the R-help mailing list