[R] Positioning a legend via X and Y coordinates

Duncan Murdoch murdoch at stats.uwo.ca
Wed May 6 19:25:37 CEST 2009


On 5/6/2009 1:08 PM, Steve Murray wrote:
> Dear R Users,
> 
> I'm able to display a legend using the following code:
> 
>> legend("topright", c("Simulation", "Observation"), fill=2:3, bty="n")
> 
> However, this causes the legend to be positioned too close to the bars in my barplot. I'd like to move the legend up slightly. I have been trying to determine the necessary values by trial and error to do this manually (by entering a coordinate) - however, I can't seem to get the legend to display! I must be off-limits, although I have tried a range of values.
> 
> I can do a temporary fix by doing locator(1), but I'd ideally like to learn how to determine the range of coordinate values 'available' within the graphics console in order to state the x and y values for legend positioning in the legend command. Is there an easy way to do this?

par("usr") gives the range of user coordinates.

You might also be able to use the "inset" argument to legend to move it. 
  inset can be one or two values; if two, they set x and y insets 
separately.  You can use negative inset values if you also include the 
xpd=NA argument.

Duncan Murdoch




More information about the R-help mailing list