[R] Changing a plot

Ben Bolker bolker at ufl.edu
Thu Sep 25 16:20:35 CEST 2008


R Help wrote:
> Thanks, I looked into the grid package.  The grid package does do a
> better job of managing the plotting, but it's still re-plotting the
> entire canvas whenever a modifcation is made to a plot.
> 
> I guess I should have been a little clearer with my question.  Here's
> a sample function.
> 
> library(tcltk)
> x = runif(10000)
> y = runif(10000)
> v1 <- viewport()
> grid.rect(gp = gpar(lty = "dashed"))
> pushViewport(plotViewport(c(5.1, 4.1, 4.1, 2.1)))
> pushViewport(dataViewport(x, y))
> grid.rect()
> grid.xaxis()
> grid.yaxis()
> grid.points(x, y)
> grid.text("1:10", x = unit(-3, "lines"), rot = 90)
> v2 <- viewport()
> push.viewport()
> grid.points(x[1],y[1],pch=16,gp=gpar(col='green'),name='pts')
> index = tclVar(1)
> grid
> refresh <- function(...){
>   i <- as.numeric(tclvalue(index))
>   grid.edit('pts',x=unit(x[i],'npc'),y=unit(y[i],'npc'))
> 
> }
> m <- tktoplevel()
> pScale <- tkscale(m,from=0,to=10000,orient='horiz',resolution=1,variable=index,command=refresh)
> tkgrid(pScale)
> 
> The green point should change as the slider is moved, but there are so
> many points in the background that replotting them confuses the
> graphic.  What I want to be able to do is replt the green point
> without removing the background.
> 
> Sam Stewart
> 

  OK, I see the issue but I don't at the moment see a way around it.
  The only R graphics device I know of that actually maintains the full
scene in the way that you want is rgl:


library(rgl)
rgl.bg(color="white")
rgl.points(runif(10000),runif(10000),0,col="black")
rgl.viewpoint(theta=0,phi=0,fov=1)
for (i in 5:995) {
  ##  rgl.points(i/1000,i/1000,0,col="green",alpha=0.5,size=20)
  rgl.spheres(i/1000,i/1000,0,col="green",alpha=1,radius=0.02)
  rgl.pop()
}

  This is kind of an ugly workaround, but maybe it will work for you.
(Really, the fundamental problem is that the plot is ALWAYS being
redrawn -- I think the issue is that rgl just redraws it much faster ...)

   I'm curious if anyone else has solutions.

  Ben Bolker



More information about the R-help mailing list