Why does plot not respect add = TRUE?

前端 未结 2 1760
醉酒成梦
醉酒成梦 2020-12-07 01:04

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

相关标签:
2条回答
  • 2020-12-07 01:38

    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
    0 讨论(0)
  • 2020-12-07 01:59

    If add=TRUE is not provided (and you are using base graphics) then use par(new=TRUE) prior to the plot call. You will need to suppress the xlab, ylab and other stuff that might interfere or overlay existing annotation, and I did leave the ylab untouched to illustrate why that warning is needed. You also will need to set xlim and ylim to be the same.

     plot(1:10);par(new=TRUE)
     plot(10:1)
    

    enter image description here

    After reviewing the comments, my vote is for @John's point that a new plot-call would perhaps have a different set of xlim and ylim, not to mention the possibility of overwriting all the text objects in the margins. points and lines do not have the facility to recalculate the limits of the plot area so they are "safe" to use with plot.default.

    0 讨论(0)
提交回复
热议问题