[R] Selecting with mouse the lines drawn by matplot()
Uwe Ligges
ligges at statistik.tu-dortmund.de
Sat May 26 17:34:02 CEST 2012
On 25.05.2012 22:52, servet cizmeli wrote:
> Dear all,
>
> I have a dataset of spectral measurements of sunlight. One row for each
> different observation, one column for each spectral channel.
>
> I would like to select the spectral curves drawn with matplot() (I only
> know matplot() as a way of easily drawing them) with the help of the
> mouse and extract their row indexes :
>
> a=url("http://ekumen.homelinux.net/spectra.RData")
> load(a)
> matplot(lbd2, t(mySpectra), type="l",xlab="Wavelength
> [nm]",ylab="Radiance")
>
> I was hoping that identify() would work on such a graphic but
> unfortunately it does not. I tried to the "identify" function of the
> package playwith(), but still the same problem.
>
> I think I will have to write a function that does the job. But before
> that, I would like to have your opinions. What would be the easiest way
> of accomplishing this task?
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
> Thank you very much
> Best regards
> servet
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list