本文介绍了R不一致:为什么add = T有时有效,有时在plot()函数中无效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么R与plot()函数中的add参数不一致?它有时有效,有时却无效!在此示例中,它毫无问题地采用了参数add=TRUE:

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))

但是当我发出

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.

请不要写我应该使用points()代替.我知道我可以用.我想了解R的奇怪行为-为什么它有时起作用而有时却不起作用?

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?

推荐答案

这固然很烦人并且前后矛盾,但这是可以解释的.

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

编辑:我不知道identity是内置对象(身份函数)的事实(因此该问题实际上是可重现的).

edit: the fact that identity is a built-in object (identity function) eluded me (so the problem is in fact reproducible).

identity是类-function的对象,该类具有plot方法(plot.function)和add自变量,而默认的plot方法没有 有一个add参数.

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.

通常,在尝试绘制对象bar时,应尝试class(bar);否则,请执行以下步骤.如果它属于类foo,则尝试methods(class="foo")可以看到它具有plot方法,或者尝试methods("plot")可以看到plot.foo存在.尝试?plot.foo查看帮助,或者尝试plot.foogetAnywhere(plot.foo)查看功能本身.

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 to see help, or plot.foo or getAnywhere(plot.foo) to see the function itself.

这篇关于R不一致:为什么add = T有时有效,有时在plot()函数中无效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 11:16