[R] How to use the function "plot" as Matlab

Paul Murrell p.murrell at auckland.ac.nz
Mon Jul 18 02:18:37 CEST 2005


Hi


Peter Dalgaard wrote:
> (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk> writes:
> 
> 
>>This is definitely a case where "dynamic rescaling" could save
>>hassle! Brian Ripley's suggestion involves first building a
>>matrix whose columns are the replications and rows the time-points,
>>and Robin Hankin's could be easily adapted to do the same,
>>though I think would involve a loop over columns and some very
>>long vectors.
>>
>>How much easier it would be with dynamic scaling!
> 
> 
> Cue grid graphics... (and Paul's new book)


... which will give you the basic tools to produce something like this. 
  Here's a very simple start at one possible way to do it (no argument 
checking, assumes x-values are 1:length(y-values), always plots points, 
quickly runs out of different symbols to use, pays no heed to 
efficiency, ...):

plotVPs <- function(x) {
   vpStack(plotViewport(c(5, 4, 4, 2), name="pvp"),
           # Calculate scale ranges based on ALL data
           dataViewport(1:max(unlist(lapply(x, length))),
                        unlist(x), name="dvp"))
}

drawDetails.scalePlot <- function(x, recording) {
   pushViewport(plotVPs(x$data))
   grid.xaxis()
   grid.yaxis()
   grid.rect()
   # Plot ALL data
   for (i in 1:length(x$data)) {
     xx <- 1:length(x$data[[i]])
     yy <- x$data[[i]]
     grid.lines(xx, yy, default.units="native",
                gp=gpar(col="grey"))
     grid.points(xx, yy, pch=i,
                 gp=gpar(cex=0.5))
   }
   upViewport(2)
}

scalePlot <- function(x, name=NULL, newpage=TRUE) {
   if (newpage)
     grid.newpage()
   grid.draw(grob(data=list(x), name=name,
                  cl="scalePlot"))
}

addPoints <- function(x, plot) {
   grid.edit(plot, data=c(grid.get(plot)$data, list(x)))
}

# Testing
scalePlot(1:10, "myplot")
addPoints(2*1:100, "myplot")
addPoints(20*sin(seq(0, 3*pi, length=50)), "myplot")

Paul
-- 
Dr Paul Murrell
Department of Statistics
The University of Auckland
Private Bag 92019
Auckland
New Zealand
64 9 3737599 x85392
paul at stat.auckland.ac.nz
http://www.stat.auckland.ac.nz/~paul/




More information about the R-help mailing list