Re(2): [R] Interacting with graphics: image

Agustin Lobo alobo at ija.csic.es
Wed Aug 1 15:44:41 CEST 2001


On Wed, 1 Aug 2001, Prof Brian D Ripley wrote:

> On Wed, 1 Aug 2001, Agustin Lobo wrote:
> 
> > After displaying a matrix x with image(x),
> > is there any way to check the x values with the
> > cursor? I've tried locator() and identify(), but does not seem
> > to be the way.
> 
> It is the way.  For example, from ?image:
> 
>      data(volcano)
>      x <- 10*(1:nrow(volcano))
>      y <- 10*(1:ncol(volcano))
>      image(x, y, volcano, col = terrain.colors(100), axes = FALSE)
> 
>      clicked <- locator()
>      volcano[cbind(round(clicked$x/10), round(clicked$y/10))]
> 
> (You will need to do the mapping to coordinates in a problem-specific way,
> and indeed this will depend on exactly hoe image was called.)

I had tried something similar, but this is non-interactive.
locator() collects n points and then outputs the result as a list. What
I'm
trying to do is to browse the image values on the screen, for which
I would need locator() to output the x,y coordinate after each
click with the mouse. If I use locator(n=1) I get the x,y coordinate,
but then the function ends. I think that an option to output one
coordinate at each click would be good in the future.

As a workaround, what I've done is the following function, ima.explore:

function (ima,n=5)    
{
        maxcol<-dim(ima)[2]
        maxlin<-dim(ima)[1]
        i <- 1
        for(i in 1:n) {
                xy <- locator(n=1)
                colu <- round(xy$x)
                lin <- maxlin - round(xy$y) + 1
                cat("Row: ",lin, "  Col: ",colu,"  Value: 
",ima[lin,colu]," \n")
                i <- i+1
        }
}

I use this function after
image(x=1:ncol(ima),y=1:nrow(ima),t(ima)[,nrow(ima):1],col=mircolors,axes=F)

which displays ima in the same way as it is printed and the output is
such:

Row:  1   Col:  1   Value:  1  
Row:  1   Col:  2   Value:  1

etc

(Note that for example:

> test.ref
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    1    1    1    1    1    1    1    2    3     3
 [2,]    1    1    1    1    1    1    2    2    2    96
 [3,]    1    1    1    1    1    1    2    2    2    96
 [4,]    1    1    1    1    1    2    2    2    2     2
 [5,]  141    1    1    1    5  142    2    2    2     2
 [6,]  141  142  142  142  142  142  142    2    2     2
 [7,]  142  142  142  142  142  142  142  142    2     2
 [8,]  206  142  142  142  142  142  142  142  142     2
 [9,]  206  206  142  142  142  142  142  142  142   142
[10,]  206  206  206  142  142  142  142  142  142    10

would be dispayed by (image(x=1:10,y=1:10,test.ref) in the
following order:

> t(test.ref)[10:1,]
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    3   96   96    2    2    2    2    2  142    10
 [2,]    3    2    2    2    2    2    2  142  142   142
 [3,]    2    2    2    2    2    2  142  142  142   142
 [4,]    1    2    2    2    2  142  142  142  142   142
 [5,]    1    1    1    2  142  142  142  142  142   142
 [6,]    1    1    1    1    5  142  142  142  142   142
 [7,]    1    1    1    1    1  142  142  142  142   142
 [8,]    1    1    1    1    1  142  142  142  142   206
 [9,]    1    1    1    1    1  142  142  142  206   206
[10,]    1    1    1    1  141  141  142  206  206   206

Agus


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list