Skip to content

troubles with displaying legend on the plot

6 messages · Jerome Asselin, Косенков Кирилл Н, Vladimir Eremeev +2 more

#
Dear colleagues,

I have troubles while trying to display legend on the plot.

I have data.frame 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.
#
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:
#
Hi! It seems, that y-coord of your legend (1979,0), (0,0) located 
outside your coordinate system. 
par(usr=c(1979,2002,20,90),mar=c(7, 4, 4, 2) + 0.1)

You can use par('usr') to determine extremes of your coordinates 
for automatic legend placing. Or you can
legend(locator(1),[other options])
to place legend on your plot interactively.
Notice, that the y-coord has its 0 in the _bottom_ of figure region.
Wladimir Eremeev wrote:
#
Dear colleagues,

Thank you, I've already realized that...

Is is possible to place the legend outside a plot in any other way
except using lattice package?

BTW, maybe it would be useful to place this question in FAQ?
I've searched the mail archive. The question about placing the legend
outside a plot already have arose...
Wednesday, April 16, 2003, 11:52:49, you wrote:
???> Hi! It seems, that y-coord of your legend (1979,0), (0,0) located 
???> outside your coordinate system. 
???> par(usr=c(1979,2002,20,90),mar=c(7, 4, 4, 2) + 0.1)

???> You can use par('usr') to determine extremes of your coordinates 
???> for automatic legend placing. Or you can
???> legend(locator(1),[other options])
???> to place legend on your plot interactively.
???> Notice, that the y-coord has its 0 in the _bottom_ of figure region.
#
On Wed, Apr 16, 2003 at 12:26:11PM +0400, Wladimir Eremeev wrote:
Sort of -- I once did that using layout(). In essence you arrange two (or
more) plots on a page. One of those plots remains 'empty' and only contains
a legend, which gives the desired of having a plot with a separate legend.
This was discussed before. Simply trying one google query as in
     http://www.google.com/search?q=legend+outside+r-help
or     
     http://www.google.com/search?q=legend+outside+site:r-project.org
shows several results, including one with the simpler answer
     par(xpd=T)

Hope this helps, Dirk
#
Another trick (which I think I got from Modern Applied Statistics with 
S) is to compute xlim manually and add 20% or 50% on the right or left 
to create the blank space you want for the legend.

hth
spencer graves
Dirk Eddelbuettel wrote: