Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does plot not respect add = TRUE?

Tags:

r

Why does R's base plot function do this? We have to use points or lines, which needs special code rather than using the type argument.

plot(1:10)
plot(10:1, add = TRUE)
Warning messages:
1: In plot.window(...) : "add" is not a graphical parameter
2: In plot.xy(xy, type, ...) : "add" is not a graphical parameter
3: In axis(side = side, at = at, labels = labels, ...) :

Etc.

Many packages provide methods for (i.e. "override") plot and provide the obvious ability to plot(obj, add = TRUE) as long as obj is of the appropriate class. (Examples are sp, raster, spatstat.)

Is there any reason plot.default does not already?

EDIT: this was discussed at length here:

https://stat.ethz.ch/pipermail/r-devel/2008-March/048628.html

DM effectively answers it here:

https://stat.ethz.ch/pipermail/r-devel/2008-March/048650.html

like image 791
mdsumner Avatar asked Aug 15 '13 21:08

mdsumner


1 Answers

Because plot.default doesn't have an add argument

> args(plot.default)
function (x, y = NULL, type = "p", xlim = NULL, ylim = NULL, 
    log = "", main = NULL, sub = NULL, xlab = NULL, ylab = NULL, 
    ann = par("ann"), axes = TRUE, frame.plot = axes, panel.first = NULL, 
    panel.last = NULL, asp = NA, ...) 
NULL

Those other functions are not overriding plot but are providing their own methods, which do have an argument add because they were written that way. Personally, having grown up with using points() and lines() etc I don't find them much extra work and I would use them in preference to a plot method with an add argument, although we've written both ways in packages that I contribute to.

As to why plot.default doesn't have an add argument? You'd have to ask R Core, but I can suggest some reasons

  1. plot.default is designed to generate an entire plot on the device
  2. There already are points() and lines() etc so why duplicate?
  3. plot.default is simpler code without code to handle add
  4. Backwards compatibility with S/S-Plus
like image 157
Gavin Simpson Avatar answered Sep 28 '22 16:09

Gavin Simpson