legend.position: automatically find an optimal position a plot legend based on...

View source: R/plots.R

legend.positionR Documentation

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)

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

Value

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

See Also

Hmisc's largest.empty function

Examples


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) 
}

caroline documentation built on Oct. 21, 2024, 9:06 a.m.