Data point locator function

December 5, 2014

(This article was first published on me nugget, and kindly contributed to R-bloggers)

Here’s a little function to select data points in an open graphical device (ptlocator()). The function does a scaling of the x and y axes in order to give them equal weighting and remove the influence of differing units or ranges. The function then calculates the Euclidean distance between the selected locations (using the locator() function) and the x, y coordinates of the plotted data points. Colored points are filled in for the data point that has the lowest distance to the clicked location, and the results give the vector positions of the closest x, y data points.

The function:

ptlocator <- function(n=1, x, y, col=rgb(1,0,0,0.5), pch=20, ...){
xsc <- scale(x)
ysc <- scale(y)
pos <- seq(n)*NaN
for(i in seq(n)){
print(paste("choose point", i))
pt <- locator(1)
ptxsc <- scale(pt$x, center=attr(xsc,"scaled:center"), scale=attr(xsc,"scaled:scale"))
ptysc <- scale(pt$y, center=attr(ysc,"scaled:center"), scale=attr(ysc,"scaled:scale"))
pos.i <- which.min(sqrt((c(ptxsc)-c(xsc))^2 + (c(ptysc)-c(ysc))^2))
points(x[pos.i], y[pos.i], col=col, pch=pch, ...)
pos[i] <- pos.i

To reproduce example:

n <- 200
x <- sort(runif(n, min=0, max=10*pi))
y <- sin(x) + rnorm(n, sd=0.2)
# Select 10 points at maxima and minima
op <- par(mar=c(4,4,1,1))
plot(x,y, cex=2)
pos <- ptlocator(10, x, y, col=rgb(1,0.2,0.2,0.75), cex=2)

To leave a comment for the author, please follow the link and comment on their blog: me nugget. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)