S also provides functions which allow users to extract or add information to a plot using a mouse. The simplest of these is the locator() function:
Waits for the user to select locations on the current plot using the left mouse button. This continues until n (default 500) points have been selected, or the middle mouse button is pressed. The type argument allows for plotting at the selected points and has the same effect as for high-level graphics commands; the default is no plotting. locator() returns the locations of the points selected as a list with two components x and y.
locator() is usually called with no arguments. It is particularly useful for interactively selecting positions for graphic elements such as legends or labels when it is difficult to calculate in advance where the graphic should be placed. For example, to place some informative text near an outlying point, the command
text(locator(1), "Outlier", adj=0)
may be useful. locator() will still work if the current device does not support a mouse; in this case the user will be prompted for x and y coordinates.
Allow the user to highlight any of the points defined by x and y (using the left mouse button) by plotting the corresponding component of labels nearby (or the index number of the point if labels is absent). Returns the indices of the selected points when the middle button is pressed.
Sometimes we want to identify particular points on a plot, rather than their positions. For example, we may wish the user to select some observation of interest from a graphical display and then manipulate that observation in some way. Given a number of (x,y) coordinates in two numeric vectors x and y, we could use the identify() function as follows:
plot(x,y)
identify(x,y)
The identify() functions performs no plotting itself, but simply allows the user to move the mouse pointer and click the left mouse button near a point. The point nearest the mouse pointer will be and highlighted with its index number (i.e. its position in the x/y vectors) plotted nearby. Alternatively, you could use some informative string (such as a case name) as a highlight by using the labels argument to identify(), or disable highlighting altogether with the plot=F argument. When the middle button is pressed, identify() returns the indices of the selected points; you can use these indices to extract the selected points from the original vectors x and y.