R: Generate coordinate data from user points?

I would like to add points manually to the plot (with a mouse click) and then generate coordinate data from these points.

Is there a package or feature set that would allow me to do this in R?

+5
source share
1 answer

For this you can use the basic function locator(). Follow these steps, 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()often useful when you need to accurately place something - text or legend, say - on a graph in which the coordinate system of the graph is not easy to read from the axes. For example, try this by clicking once before returning to the command line:

barplot(VADeaths)
text(locator(1), "I clicked here", col="red")
+6
source

All Articles