Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R inconsistency: why add=T sometimes works and sometimes not in the plot() function?

Tags:

plot

r

add

Why is R inconsistent with the add parameter in the plot() function? It sometimes works and sometimes doesn't! In this example, it takes the parameter add=TRUE with no problem:

plot(0:10, 0:10*3) plot(identity, add=TRUE, xlim=c(0,10)) plot(function (x) { sin(x)*10 }, add=TRUE, xlim=c(0,10)) 

But when I issue

plot(c(2, 3, 4), c(20,10,15), add=TRUE, pch="A") 

It doesn't work!! It says that "add" is not a graphical parameter.

Please do not write that I should use points() instead. I know I can use it. I want to understand the strange behaviour of R - why does it sometimes work and sometimes not?

like image 490
Tomas Avatar asked Jul 22 '11 11:07

Tomas


1 Answers

This is admittedly annoying and inconsistent, but it's explicable.

identity is an object of a class — function — that has a plot method (plot.function) with an add argument, while the default plot method does not have an add argument.

In general, when trying to plot object bar, you should try class(bar); if it is of class foo then try methods(class="foo") to see that it has a plot method, or methods("plot") to see that plot.foo exists. Try ?plot.foo (or help("plot.foo") to see help, or plot.foo to see the function itself. (If the method is a private function in the package mypkg you may need mypkg:::plot_foo or or getAnywhere(plot.foo) to find it.)

like image 152
Ben Bolker Avatar answered Sep 18 '22 04:09

Ben Bolker