Skip to content
Prev 295723 / 398502 Next

Selecting with mouse the lines drawn by matplot()

On 25.05.2012 22:52, servet cizmeli wrote:
Write a little function that makes use of locator():

Take its x position and calculate (by interpolation) the y location of 
all paths at this x position, then take the minimal distance.
The idea in quick an dirty:


mat_identify <- function(x, y, ...){
     l <- locator(1)
     if(all(x <= l$x) || all(x >= l$x))
         stop("not within data range")
     index <- max(which(x <= l$x))
     f <- (l$x - x[index]) / diff(x[index+(0:1)])
     yi <- f * (y[index+1,] - y[index,] ) + y[index,]
     result <- which.min(abs(yi-l$y))
     lines(x, y[,result], lwd=2, col="red")
     text(l, label=rownames(y)[result])
     result
}

matplot(lbd2, t(mySpectra), type="l",xlab="Wavelength [nm]",ylab="Radiance")
mat_identify(lbd2, t(mySpectra))

Best,
Uwe Ligges