[R] legend question
Stephen Tucker
brown_emu at yahoo.com
Wed Feb 28 17:32:20 CET 2007
Hi Emili,
Even though you are calling your horizontal coordinate y, and vertical
coordinate z, the first and second arguments to legend(), namely x and y,
should be the horizontal and vertical coordinates, respectively; and they are
given in user coordinates (e.g., legend()'s x should be between 1960 and 1975
and legend()'s y should be between 1 and 4).
If you want to use normalized coordinates (i.e. 0 to 1), you can scale as in
this example:
legend(x = par("usr")[1] + diff(par("usr")[1:2])*normalizedCoordX,
y = par("usr")[3] + diff(par("usr")[3:4])*normalizedCoordY,
...)
where normalizedCoordX and Y go from 0 to 1 (see ?par, par("usr") returns
vector of c(xmin,xmax,ymin,ymax) of user coordinates on a plot)
You can alternatively use legend(x = "topleft",...) or "bottomright", and so
on to place your legend.
If you want to add your legend outside of the plot, you should consider
increasing the margins using the 'mar' argument in par(), and also setting
par(xpd=TRUE) (so stuff can show up outside of the plotting region).
Best regards,
ST
> y<-c(1960, 1965, 1970, 1975)
> z<-c(1, 2, 3, 4)
within the data limits of your x and y)
--- Emili Tortosa-Ausina <Emili.Tortosa at eco.uji.es> wrote:
> Hi to all,
>
> I'm sorry for posting this question, I am sure I am missing something
> important but after reading the documentation I cannot find where the
> problem is.
>
> I want to add a legend to a figure. If I use a simple example drawn
> from the R Reference Manual such as, for instance:
>
> x <- seq(-pi, pi, len = 65)
> plot(x, sin(x), type="l", col = 2)
> legend(x = -3, y = .9, "legend text", pch = 1, xjust = 0.5)
>
> then everything works just fine.
>
> However, if I use other data such as, for instance:
>
> y<-c(1960, 1965, 1970, 1975)
> z<-c(1, 2, 3, 4)
> plot(y, z, type="l", col = 2)
> legend(x = -3, y = .9, "legend text", pch = 1, xjust = 0.5)
>
> then the legend is not shown.
>
> Any hints?
>
> Thanks in advance,
>
> Emili
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
____________________________________________________________________________________
Food fight? Enjoy some healthy debate
More information about the R-help
mailing list