# [R] using R interactively

Terry J. Westley twestley at buffalo.veridian.com
Wed Jun 30 22:53:02 CEST 1999

Thanks for your suggestions to my questions:
> 1) Is it possible to erase data points, legends, and/or text
>    from an R plot?

Several of you suggested to the method I finally chose: rewrite
the data point to be erased with the background color.

> 2) What's the best way to find the closest data point to
>    what locator() returns?

Thanks to Charles Berry for his suggestion on using the Euclidian
distance method.  I had to modify his idea to convert the position
of my data points to inches in order to account for non-square
plots and non-equal axes lengths.  Below is the code for a simple
test version of what I did.

--
Terry J. Westley, Principal Engineer
Veridian Engineering, Calspan Operations
P.O. Box 400, Buffalo, NY 14225
twestley at buffalo.veridian.com    http://www.veridian.com

#-----------------------------------------------------------------------
loc.prev <- list(x=NA, y=NA)

# Creates two vectors, x and y, of n runif
# data points and plots them -- this is just
# used for testing functions plotdist and
# closest.
#------------------------------------------
newplot <- function (n) {
x <<- 1:n
y <<- runif(n)
plot(x, y, pch="o")
}

# Computes vector of distances between
# x0,y0 and x1,y1 data points.  Get usr
# and pin each time to account for user
# resizing the window.
#-----------------------------------------
plotdist <- function (x0, y0, x1, y1, usr=par("usr"), pin=par("pin")) {
# xpi means X coordinates Per Inch
xpi <- (usr[2] - usr[1]) / pin[1]
ypi <- (usr[4] - usr[3]) / pin[2]
(((x0-x1)/xpi)^2 + ((y0-y1)/ypi)^2)^0.5
}

# Determines which vector element in x,y is
# closest to lx, ly.
#------------------------------------------
closest <- function (x, y, lx, ly) {
dist <- plotdist(x, y, lx, ly)
dist.min <- min(dist)
seq(along=x)[dist == dist.min]
}

# Let user click on a plot, then show
# details of the data point.
#------------------------------------
show <- function () {
while (1) {

# get location user clicked on and
# determine index in x,y
#---------------------------------
l <- locator(1)
index <- closest(x, y, l\$x, l\$y)

# Erase marker on previous point,
# redraw previous point and save
# newly selected data point for
# next erase.
#-------------------------------
if (!is.na(loc.prev\$x)) {
points(x=loc.prev\$x, y=loc.prev\$y,
pch="X", col="white")
points(x=loc.prev\$x, y=loc.prev\$y,
pch="o", col="black")
}
loc.prev <- list(x=x[index], y=y[index])

# Display a marker of which data point
# the user selected and a legend with
# details of the data.
#-------------------------------------
points(x=x[index], y=y[index], pch="X")
legend (x=1, y=max(y),
legend=paste("y[", index, "]=", y[index], sep=""))
}
}

# demonstrate with a plot of 50 random numbers
#---------------------------------------------
newplot(50)
show()

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._