Hello, I've written a function that plots a few functions in a diagram. The xlim and or ylim is not always the same, and set automatically by R. A legend is part of this object. Now the problem is: where to put the legend? Me would help a function that returns the limits and scaling of the axis. Thanks for your help. Carsten [[alternative HTML version deleted]]
Carsten Steinhoff wrote:> Hello, > > I've written a function that plots a few functions in a diagram. > The xlim and or ylim is not always the same, and set automatically by R. > A legend is part of this object. > Now the problem is: where to put the legend? Me would help a function that > returns the limits and scaling of the axis.See ?par, in particular par("usr") Uwe Ligges> Thanks for your help. > > Carsten > > [[alternative HTML version deleted]] > > ______________________________________________ > 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
On Thu, 2005-06-09 at 19:05 +0200, Carsten Steinhoff wrote:> Hello, > > I've written a function that plots a few functions in a diagram. > The xlim and or ylim is not always the same, and set automatically by R. > A legend is part of this object. > Now the problem is: where to put the legend? Me would help a function that > returns the limits and scaling of the axis. > > Thanks for your help. > > CarstenYou can explicitly set the xlim and ylim values in most plotting functions. If your function is based upon an underlying R function, just pass xlim and ylim as arguments from your function so that you can "leave room" for a legend. See ?plot.default for an example. Alternatively, using par("usr") will get you the ranges of the axes once a plot is created:> plot(1:10)# c(x1, x2, y1, y2)> par("usr")[1] 0.64 10.36 0.64 10.36 See ?par for more information. Note that if you might be using log scaling on one or both axes, the output of par("usr") needs to be adjusted:> plot(1:10, log = "xy")> par("usr")[1] -0.04 1.04 -0.04 1.04 # Use this correction for both axes in this case # or just: # 10 ^ par("usr")[1:2] for x # 10 ^ par("usr")[3:4] for y> 10 ^ par("usr")[1] 0.9120108 10.9647820 0.9120108 10.9647820 HTH, Marc Schwartz
You can use locator(1) as an argument in the "legend" function to manually position the legend, i.e. after your plot is plotted, the system will wait for you to indicate (by clicking the mouse) where the legend should be placed. legend(locator(1), ...) Hope this helps, Ravi. -------------------------------------------------------------------------- Ravi Varadhan, Ph.D. Assistant Professor, The Center on Aging and Health Division of Geriatric Medicine and Gerontology Johns Hopkins University Ph: (410) 502-2619 Fax: (410) 614-9625 Email: rvaradhan at jhmi.edu --------------------------------------------------------------------------> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch [mailto:r-help- > bounces at stat.math.ethz.ch] On Behalf Of Carsten Steinhoff > Sent: Thursday, June 09, 2005 1:06 PM > To: r-help at stat.math.ethz.ch > Subject: [R] position of a legend-object > > Hello, > > I've written a function that plots a few functions in a diagram. > The xlim and or ylim is not always the same, and set automatically by R. > A legend is part of this object. > Now the problem is: where to put the legend? Me would help a function that > returns the limits and scaling of the axis. > > Thanks for your help. > > Carsten > > [[alternative HTML version deleted]] > > ______________________________________________ > 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
On 6/9/2005 1:05 PM, Carsten Steinhoff wrote:> Hello, > > I've written a function that plots a few functions in a diagram. > The xlim and or ylim is not always the same, and set automatically by R. > A legend is part of this object. > Now the problem is: where to put the legend? Me would help a function that > returns the limits and scaling of the axis.Besides the suggestions other have sent, you might find something like legend("topright", ...) (which puts it at the top right of the plot) does what you want. This keyword positioning was new in 2.1.0. Duncan Murdoch