[R] Polygon-like interactive selection of plotted points

Roger Bivand Roger.Bivand at nhh.no
Wed Apr 26 21:08:58 CEST 2006


On Wed, 26 Apr 2006, Marc Schwartz (via MN) wrote:

> On Wed, 2006-04-26 at 18:13 +0100, Florian Nigsch wrote:
> > [Please CC me for all replies, since I am not currently subscribed to  
> > the list.]
> > 
> > Hi all,
> > 
> > I have the following problem/question: Imagine you have a two- 
> > dimensional plot, and you want to select a number of points, around  
> > which you could draw a polygon. The points of the polygon are defined  
> > by clicking in the graphics window (locator()/identify()), all points  
> > inside the polygon are returned as an object.
> > 
> > Is something like this already implemented?
> > 
> > Thanks a lot in advance,
> > 
> > Florian
> 
> I don't know if anyone has created a single function do to this (though
> it is always possible). 
> 
> However, using:
> 
>   RSiteSearch("points inside polygon")
> 
> brings up several function hits that, if put together with the above
> interactive functions, could be used to do what you wish. That is, input
> the matrix of x,y coords of the interactively selected polygon and the
> x,y coords of the underlying points set to return the points inside or
> outside the polygon boundaries.

This sequence from the splancs package should do it:

library(splancs)
set.seed(20060426)
xy <- cbind(x=runif(100), y=runif(100))
plot(xy)
poly <- getpoly() # this gets the polygon on-screen
plot(xy)
polygon(poly)
io <- inout(xy, poly) 
# this returns a logical vector for points in the polygon
points(xy[io,], pch=16, col="blue")

Roger

> 
> Just as an FYI, you might also want to look at ?chull, which is in the
> base R distribution and returns the set of points on the convex hull of
> the underlying point set. This is to some extent, the inverse of what
> you wish to do.
> 
> HTH,
> 
> Marc Schwartz
> 

-- 
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no




More information about the R-help mailing list