R plot undo line segment?

2019-02-27 03:09发布

问题:

Is there a quick and easy function to undo a segment or remove it from your plot?

    p1 <- locator(1)
    p2 <- locator(1)
    segments(p1$x, p1$y, p2$x, p2$y, col = 'pink')
    //Undo segments

What I mean to say is, is it possible to store the line segment (color/intensity of each pixel) you are about to erase, and then later on add that line segment where the pink one was to in effect undo the pink segment draw. How would one accomplish this?

回答1:

You can do it with Grid graphics,

library(grid)
ll = replicate(2, grid.locator())
g = grid.segments(ll[,1]$x, ll[,1]$y, ll[,2]$x, ll[,2]$y, 
      name="mysegment", gp=gpar(col="pink", lwd=5))
grid.remove("mysegment")


回答2:

No. About the best you can do unless you use grid graphics is to write over the offending segment in the background color (i.e. segments(p1$x, p1$y, p2$x, p2$y, col = 'white') if the background is white -- a hack that sometimes fails).

To address your updated question above about somehow storing the written pixels to remove them later; in R's base graphics system (as distinguished from Paul Murrell's grid graphics system, as discussed by @baptiste, or graphics using the rgl package for dynamic 3D graphics) the metaphor is of a canvas you're painting. R doesn't "remember" what it has drawn; those pixels or segments are added to the output device and forgotten.



标签: r segment