Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: Generate coordinate data from user-drawn points?

I would like to add points manually to a plot (through mouse clicks) and then generate coordinate data from those points.

Is there a package or a set of functions that would let me do this in R?

like image 394
Mike Furlender Avatar asked Feb 24 '12 00:02

Mike Furlender


1 Answers

You can use the base function locator() to do this. Try the following, for example:

plot(1:4)
df <- data.frame(locator())
## Now, on the plotting device:
## 
##     (1) "Left-click" on each of the four points
##     (2) "Right-click --> Stop" to return to the command-line

## The object that is returned, and assigned to df will look
## something like the following
df
         x        y
1 1.008072 1.032795
2 2.011049 2.002365
3 3.004381 2.995299
4 3.997714 4.011595

locator() is often useful when you need to precisely place something -- text or a legend, say -- onto a plot in which the coordinate system of the plot isn't easy to read off of the axes. For instance, try this, clicking once before returning to the command-line:

barplot(VADeaths)
text(locator(1), "I clicked here", col="red")
like image 191
Josh O'Brien Avatar answered Sep 30 '22 14:09

Josh O'Brien