Re: [R] retrieve x y coordinates of points in current plot

From: Jannis <>
Date: Sat, 05 Mar 2011 19:54:28 +0100

On 03/05/2011 07:25 PM, Greg Snow wrote:
> It is not completely clear what you are trying to accomplish. Do you want to draw a shape in the plot then identify all the points in that shape? You could use locator (with type='l') to draw a polygon, then there are functions in add on packages (mostly the spatial ones) that will detect which points are within a polygon that you could use with the raw data and the polygon created.
> If that is not what you want, then maybe describe your goals in more detail (examples are good if you can give one).
Thats exactly what I want. drawing a polygon in a plot and searching for the points inside the polygon. I managed to create that polygon and to check which points are inside but only by supplying my function with the coordinates of the points. Now I was wondering whether it is also possible to retrieve these coordinates from the plot (similar to par()$usr and similar...).

ideally it would work as follows:

plot(x,y) <- identify.poly() #see below #now click on the plot to identify the points

Right now it only works like <- identify.poly(x,y)

Anyway, supplying the points is not too complicated, it would just be easier to do without.

identify.poly <- function(x,y,col.points='red') {


   coords.all <- list(x=vector(length=100),y=vector(length=100))

   while (i<100)
       coords.t <- locator(n=1)
       if (exit)

       coords.all$x[i] <- coords.t$x
       coords.all$y[i] <- coords.t$y
       if (i>1)

   coords.all$x <- na.omit(coords.all$x)    coords.all$y <- na.omit(coords.all$y)

   points.inpoly <-,point.y=y,



   data.return=list(in.poly=!!points.inpoly,x=x,y=y) } mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Sat 05 Mar 2011 - 19:00:19 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 05 Mar 2011 - 19:30:19 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive