For this you can use the basic function locator(). Follow these steps, for example:
plot(1:4)
df <- data.frame(locator())
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")
source
share