Learn R Programming

caroline (version 0.9.9)

legend.position: automatically find an optimal position a plot legend based on point counts in each plot corner

Description

This function assesses four corners of an x vs y plot to find the one with the fewest points. It does so by iterating counting from outward to the middle, performing a sparseness-weighted average across corners.

Usage

legend.position(x,y,xlim=NULL,ylim=NULL,start=.05, end=.5, incr=.01)

Value

one of four values ('bottom'/'top' + 'right'/'left') corresponding to the optimal [most white space] position of the legend.

Arguments

x

x values of the plot

y

y values of the plot

xlim

range of the x values

ylim

range of the y values

start

starting percentage for incremental counting from corner

end

ending percentage for incremental counting (middle=0.5)

incr

incremental step between each point counting iteration

See Also

Hmisc's largest.empty function

Examples

Run this code

par(mfrow=c(3,3))

for(i in 1:9){
x <- rnorm(50)
y <- rlnorm(50)
clrs <- c('red','blue')
plot(x,y, col=clrs)
legend(x=legend.position(x,y), legend = c('x','y'), pch=1, col=clrs, inset=.1) 
}

Run the code above in your browser using DataLab