I'm interested in writing a function that constructs a new plot on the current graphics device if no plot exists there yet, but adds lines to the existing plot if a plot is already there. How can I do this? It seems to me that the exists() function might be co-opted to do this, but it's not obvious how. Many thanks, Roger -- Roger Levy Email: rlevy at ucsd.edu Assistant Professor Phone: 858-534-7219 Department of Linguistics Fax: 858-534-4789 UC San Diego Web: http://ling.ucsd.edu/~rlevy
Try this: if(length(dev.list()) == 0) plot(rnorm(100), type="l") else (lines(rnorm(100))) On 07/12/2007, Roger Levy <rlevy at ucsd.edu> wrote:> I'm interested in writing a function that constructs a new plot on the > current graphics device if no plot exists there yet, but adds lines to > the existing plot if a plot is already there. How can I do this? It > seems to me that the exists() function might be co-opted to do this, but > it's not obvious how. > > Many thanks, > > Roger > > -- > > Roger Levy Email: rlevy at ucsd.edu > Assistant Professor Phone: 858-534-7219 > Department of Linguistics Fax: 858-534-4789 > UC San Diego Web: http://ling.ucsd.edu/~rlevy > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
On Fri, 7 Dec 2007, Roger Levy wrote:> I'm interested in writing a function that constructs a new plot on the > current graphics device if no plot exists there yet, but adds lines to > the existing plot if a plot is already there. How can I do this? It > seems to me that the exists() function might be co-opted to do this, but > it's not obvious how.exists() will not help with graphics devices, whose state is not stored in R objects that exists() can test for. Note that it is rare for a graphics device to be open and not contain a plot. But in those circumstances par("usr") will be c(0,1,0,1) which would be unusual after a plot. A simple way to test if a non-null device is active is dev.cur() > 1. However, I doubt if you want to add lines to any old plot that happens to be on the device, and there is no general way to tell if the existing plot is suitable (it need not be the last plot made, for example). So I can only see this goal as achievable within a constrained set of circumstances. (A further complication is that a graphics device can display either base or grid graphics, and you can't add base graphics to a grid plot or v.v.) -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
On Dec 7, 2007 4:28 AM, Roger Levy <rlevy at ucsd.edu> wrote:> I'm interested in writing a function that constructs a new plot on the > current graphics device if no plot exists there yet, but adds lines to > the existing plot if a plot is already there. How can I do this? It > seems to me that the exists() function might be co-opted to do this, but > it's not obvious how.You might want to look at the ggplot2 package, http://had.co.nz/ggplot2/, which exposes plots as objects that can be easily manipulated. Hadley -- http://had.co.nz/
The simplest way would be to have a flag, an indicator variable that stores a value that indicates if a plot has been done before. Something like this plot (do my first plot here...) is.plot=T .... later in the code... if (is.plot) {plot (do new plot here)} else {lines(add lines to the previous plot)} Julian Roger Levy wrote:> I'm interested in writing a function that constructs a new plot on the > current graphics device if no plot exists there yet, but adds lines to > the existing plot if a plot is already there. How can I do this? It > seems to me that the exists() function might be co-opted to do this, but > it's not obvious how. > > Many thanks, > > Roger > >-- Julian M. Burgos Fisheries Acoustics Research Lab School of Aquatic and Fishery Science University of Washington 1122 NE Boat Street Seattle, WA 98105 Phone: 206-221-6864