Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
546 views
in Technique[技术] by (71.8m points)

r - Why does plot not respect add = TRUE?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...