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?
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With