retrieve x y coordinates of points in current plot

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

retrieve x y coordinates of points in current plot

jannis-2
Dear list,

is it somehow possible to retrieve the x and y coordinates of points in
a scatterplot after it has been plotted? identify() somehow seems to
manage this, so I was wondering whether it is possible?

I am asking as I wrote a function that identifies points inside a
polygon and I would like it to work without supplying the x and y
coordinates again but by retrieving them from the device.


Cheers
Jannis

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: retrieve x y coordinates of points in current plot

Dieter Menne
jannis-2 wrote
is it somehow possible to retrieve the x and y coordinates of points in
a scatterplot after it has been plotted? identify() somehow seems to
manage this, so I was wondering whether it is possible?
locator might be the more basic function you are looking for.

Dieter
Reply | Threaded
Open this post in threaded view
|

Re: retrieve x y coordinates of points in current plot

jannis-2
Thanks for your replies, Dieter and Richard. I am aware of these two
functions. I wanted, however, to write a similar function to identify by
selecting large clouds of points. For this I would need to rertrieve
their coordinates after the plot was called and created. As identify()
is able to do this, I was wondering whether my function could do this as
well in a similar manner. Until now, I have to supply the x and y
coordinates to my function as well. I would like to avoid this.


Jannis

On 03/04/2011 06:55 PM, Dieter Menne wrote:

> jannis-2 wrote:
>>
>> is it somehow possible to retrieve the x and y coordinates of points in
>> a scatterplot after it has been plotted? identify() somehow seems to
>> manage this, so I was wondering whether it is possible?
>>
> locator might be the more basic function you are looking for.
>
> Dieter
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/retrieve-x-y-coordinates-of-points-in-current-plot-tp3335642p3335692.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: retrieve x y coordinates of points in current plot

Greg Snow-2
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).

--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
[hidden email]
801.408.8111


> -----Original Message-----
> From: [hidden email] [mailto:r-help-bounces@r-
> project.org] On Behalf Of Jannis
> Sent: Saturday, March 05, 2011 7:29 AM
> To: Dieter Menne
> Cc: [hidden email]
> Subject: Re: [R] retrieve x y coordinates of points in current plot
>
> Thanks for your replies, Dieter and Richard. I am aware of these two
> functions. I wanted, however, to write a similar function to identify
> by
> selecting large clouds of points. For this I would need to rertrieve
> their coordinates after the plot was called and created. As identify()
> is able to do this, I was wondering whether my function could do this
> as
> well in a similar manner. Until now, I have to supply the x and y
> coordinates to my function as well. I would like to avoid this.
>
>
> Jannis
>
> On 03/04/2011 06:55 PM, Dieter Menne wrote:
> > jannis-2 wrote:
> >>
> >> is it somehow possible to retrieve the x and y coordinates of points
> in
> >> a scatterplot after it has been plotted? identify() somehow seems to
> >> manage this, so I was wondering whether it is possible?
> >>
> > locator might be the more basic function you are looking for.
> >
> > Dieter
> >
> >
> > --
> > View this message in context: http://r.789695.n4.nabble.com/retrieve-
> x-y-coordinates-of-points-in-current-plot-tp3335642p3335692.html
> > Sent from the R help mailing list archive at Nabble.com.
> >
> > ______________________________________________
> > [hidden email] 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.
> >
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: retrieve x y coordinates of points in current plot

jannis-2
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:

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

Right now it only works like
points.in.poly <- 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')
{
   require(sp)
   exit=FALSE
   i=0
   coords.all <- list(x=vector(length=100),y=vector(length=100))
   coords.all$x[1:100]<-NA
   coords.all$y[1:100]<-NA

   while (i<100)
     {
       coords.t <- locator(n=1)
       exit=!point.in.polygon(coords.t$x,coords.t$y,
                              par()$usr[c(1,2,2,1)],par()$usr[c(3,3,4,4)])
       if (exit)
         {
           break
         }
       i=i+1
       points(coords.t,col=col.points,pch='+')

       coords.all$x[i] <- coords.t$x
       coords.all$y[i] <- coords.t$y
       if (i>1)
         points(coords.all$x[(i-1):i],coords.all$y[(i-1):i],lty=2,
                col=col.points,type='l')
     }
   points(coords.all$x[c(i,1)],coords.all$y[c(i,1)],lty=2,
          col=col.points,type='l')
   coords.all$x <- na.omit(coords.all$x)
   coords.all$y <- na.omit(coords.all$y)


   points.inpoly <- point.in.polygon(point.x=x,point.y=y,
                                     pol.x=coords.all$x,pol.y=coords.all$y)
   
points(x[points.inpoly==1],y[points.inpoly==1],pch=par()$pch,col=col.points)
   data.return=list(in.poly=!!points.inpoly,x=x,y=y)
}

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: retrieve x y coordinates of points in current plot

Greg Snow-2
Theoretically possible, but it is going to be easiest to just supply the original data (like you do when you run identify).

You can look at the output of the plot2script function in the TeachingDemos package to see the idea.  That function creates a script which will recreate the current plot (base graphics), within the script are the coordinates of the points, but also a bunch of other stuff, so you would need to parse all of that to find just the part that you want, then do what you did before.

What conditions are you in that you have the plot but not easy access to the data points plotted?

--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
[hidden email]
801.408.8111


> -----Original Message-----
> From: Jannis [mailto:[hidden email]]
> Sent: Saturday, March 05, 2011 11:54 AM
> To: Greg Snow
> Cc: Dieter Menne; [hidden email]
> Subject: Re: [R] retrieve x y coordinates of points in current plot
>
> 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:
>
> x<-rnorm(20)
> y=rnorm(20)
> plot(x,y)
> points.in.poly <- identify.poly() #see below
> #now click on the plot to identify the points
>
> Right now it only works like
> points.in.poly <- 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')
> {
>    require(sp)
>    exit=FALSE
>    i=0
>    coords.all <- list(x=vector(length=100),y=vector(length=100))
>    coords.all$x[1:100]<-NA
>    coords.all$y[1:100]<-NA
>
>    while (i<100)
>      {
>        coords.t <- locator(n=1)
>        exit=!point.in.polygon(coords.t$x,coords.t$y,
>
> par()$usr[c(1,2,2,1)],par()$usr[c(3,3,4,4)])
>        if (exit)
>          {
>            break
>          }
>        i=i+1
>        points(coords.t,col=col.points,pch='+')
>
>        coords.all$x[i] <- coords.t$x
>        coords.all$y[i] <- coords.t$y
>        if (i>1)
>          points(coords.all$x[(i-1):i],coords.all$y[(i-1):i],lty=2,
>                 col=col.points,type='l')
>      }
>    points(coords.all$x[c(i,1)],coords.all$y[c(i,1)],lty=2,
>           col=col.points,type='l')
>    coords.all$x <- na.omit(coords.all$x)
>    coords.all$y <- na.omit(coords.all$y)
>
>
>    points.inpoly <- point.in.polygon(point.x=x,point.y=y,
>
> pol.x=coords.all$x,pol.y=coords.all$y)
>
> points(x[points.inpoly==1],y[points.inpoly==1],pch=par()$pch,col=col.po
> ints)
>    data.return=list(in.poly=!!points.inpoly,x=x,y=y)
> }

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: retrieve x y coordinates of points in current plot

Carl Witthoft
In reply to this post by jannis-2
If you want to identify the coordinates of points within a region, you
might want to look at the ImageJ application.

http://rsbweb.nih.gov/ij/

IIRC it has some macros to find and classify spots.

______________________________________________
[hidden email] 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.