[Rd] [patch] add=TRUE in plot.default()

Andrew Clausen clausen at econ.upenn.edu
Sun Mar 9 17:30:12 CET 2008


Hi Henrique,

Sorry I wasn't very clear.  I should have said that I think it's nice that
add=TRUE works for some things, like

	plot(f)
	plot(g, add=TRUE)

I think it's a convenient and simple way of adding things to a plot.  But this
add=TRUE trick doesn't work for everything, and I think it would be convenient
if it did.

In the past, I've used

	f <- function(x) x^2
	X <- seq(0, 1, by=1/4)
	plot(f, col="blue")
	lines(X, f(X), col="red")

which I think is clumsy.

I didn't know about "new".  Why is new back-to-front?  If you specify
"new=TRUE", it does NOT create a new graph.  Why does it require you
to specify empty xlab and ylab?  Why doesn't this work:

	f <- function(x) x^2
	X <- seq(0, 1, by=1/4)
	plot(f, col="blue")
	plot(X, f(X), col="red", type="l", xlab="", ylab="", new=TRUE)

It seems like we can't solve the first problem with new=TRUE (that it's
back-to-front) without breaking backward compatibility.  So I think add=TRUE
should be encouraged instead, as in my patch.

But, there is one extra thing to think about.  Sometimes, it's useful to
have a for-loop for putting several functions on a graph.  For example,

	f <- function(a) function(x) a * x^2
	plot.new()
	for (a in c(1,2,3))
		plot(f(a), add=TRUE, col=a)

This doesn't work well.  For example, it doesn't draw the axes.  Is there
a user-friendly way of doing this properly, short of

	f <- function(a) function(x) a * x^2
	plot(f(3), col=3)
	for (a in 1:3)
		plot(f(a), add=TRUE, col=a)

For instance, is there a command that can tell you if the axes have been
drawn?  If there were, then you could do something like

	f <- function(a) function(x) a * x^2
	plot.new()
	for (a in c(1,2,3))
		plot(f(a), add=(TRUE & par("axes.already.drawn")), col=a)

Cheers,
Andrew

On Sun, Mar 09, 2008 at 12:08:59PM -0300, Henrique Dallazuanna wrote:
> I think you can use par(new = T) here:
> 
> f <- function(x) x^2
> X <- seq(0, 1, by=1/4)
> plot(f, col="blue")
> par(new = T)
> plot(X, f(X), col="red", type="l", xlab="", ylab="")
> 
> On 09/03/2008, Andrew Clausen <clausen at econ.upenn.edu> wrote:
> > Hi all,
> >
> > As long as I've used R, add=TRUE hasn't worked in contexts like this:
> >
> > 	f <- function(x) x^2
> > 	X <- seq(0, 1, by=1/4)
> > 	plot(f, col="blue")
> > 	plot(X, f(X), col="red", type="l", add=TRUE)
> >
> > I attached a fix for version 2.6.2.
> >
> > Cheers,
> > Andrew
> >
> >
> 
> 
> -- 
> Henrique Dallazuanna
> Curitiba-Paraná-Brasil
> 25° 25' 40" S 49° 16' 22" O



More information about the R-devel mailing list