1) Is it possible to erase data points, legends, and/or text from an R plot? 2) What's the best way to find the closest data point to what locator() returns? For more background and some context on these questions, read on... In using R interactively, I am displaying further details about a specific data point when the user clicks on the plot point. Here's a simple example: # begin example n <- runif(10) plot(n) while (1) { # get index of data point user clicks on l <- locator(1) index <- order(abs(n-l$y))[1] # show which one user clicked on points (x=index, y=n[index], pch="X") # show details about that data point legend (x=1, y=max(n), legend=paste("n(", index, ") = ", n[index], sep="")) } # end example 1) Is there a way to remove or erase the data point used to identify which data point was selected and the legend when the user clicks on the next data point? 2) Is there a better way to find the closest data point than what I used: order(abs(n-l$y))[1]? I'm pretty new to R so I'm sure there are better ways than this. In my actual code, the X axis is time so I searched in the time vector rather than in y as in the example above. I'm sure there must be an easy way to find the closest point in both X and Y space. 3) Also, has anyone implemented anything like buttons or pull-down menus so that I could implement other functions? I'm using Tcl/Tk to control this application but I would prefer that, once the plot is displayed, the user doesn't have to go back to another window to invoke other functions (such as Quit). I'm using R version 0.64.0 on sparc-sun-solaris2.5.1. The sample code above also works as expected on Windows 95 with R version 0.63.3. Thanks. -- 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 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
"Terry J. Westley" <twestley at buffalo.veridian.com> writes:> 1) Is it possible to erase data points, legends, and/or text > from an R plot?Unfortunately no. R is still pretty much stuck in the old hardcopy device model. Not that we wouldn't want something smarter, but the changes would be pretty pervasive.> 2) What's the best way to find the closest data point to > what locator() returns?identify()> 3) Also, has anyone implemented anything like buttons or pull-down > menus so that I could implement other functions? I'm using > Tcl/Tk to control this application but I would prefer that, once > the plot is displayed, the user doesn't have to go back to another > window to invoke other functions (such as Quit).Guido Masarotto did a sketchy, but rather neat Tcl/Tk <-> R glue package at some point. Doesn't work with some linuxen if one uses international charset, but you probably couldn't care less about that... -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Mon, 28 Jun 1999, Terry J. Westley wrote:> 1) Is it possible to erase data points, legends, and/or text > from an R plot?The tacky but possibly workable method is to re-plot the data/legends/etc. in the background color ... Ben -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._