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

From: Paul Murrell <p.murrell_at_auckland.ac.nz>
Date: Mon 18 Jul 2005 - 10:18:37 EST

Hi

Peter Dalgaard wrote:

> (Ted Harding) <Ted.Harding@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@stat.auckland.ac.nz
http://www.stat.auckland.ac.nz/~paul/

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Mon Jul 18 10:24:46 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:33:46 EST