[R] Question about curve function

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Jun 6 11:22:25 CEST 2011


As a further example of the trickiness, the "function" method of 
plot() relies on curve(x, ...) being a request to plot the function 
x(x) against x.  I've added a comment to that effect to the help page.

On Mon, 6 Jun 2011, Prof Brian Ripley wrote:

> On Sun, 5 Jun 2011, Abhilash Balakrishnan wrote:
>
>> Dear Mr. Murdoch,
>> 
>> I find out that still do not understand why the following does not work:
>> 
>>> curve(expression(x))
>> Error in xy.coords(x, y, xlabel, ylabel, log) :
>>  'x' and 'y' lengths differ
>> 
>> As here the input to curve is an expression, as documented in the help, and
>
> Not really, and certainly not in the sense you seem to understand it.. 
> 'expression(x)' is a call to the expression() function, and that evaluates to 
> a length-one expression vector. As ?expression says:
>
>     ‘Expression’ here is not being used in its colloquial sense, that
>     of mathematical expressions.  Those are calls (see ‘call’) in R,
>     and an R expression vector is a list of calls, symbols etc, for
>     example as returned by ‘parse’.
>
>> the expression is simply x.
>
> 'Simply' untrue.
>
>> What is the y mentioned in the error?  There is no y used here.
>
> Yes, there is.  Please do read the code for 'curve':
>
>    y <- eval(expr, envir = list(x = x), enclos = parent.frame())
>
> so you are trying to plot a length-1 expression vector against a length-101 
> 'x'.
>
> As others have said, curve() is a convenience function, and its requirements 
> are rather picky.  And you have already been given one good solution, 
> curve(I).
>
>> Thank you for support.
>> Abhilash B.
>> 
>> 
>> On Sun, Jun 5, 2011 at 3:39 PM, Duncan Murdoch 
>> <murdoch.duncan at gmail.com>wrote:
>> 
>>> On 11-06-05 1:07 PM, Abhilash Balakrishnan wrote:
>>> 
>>>> Dear Sirs,
>>>> 
>>>> I am a new user of the R package.  When I try to use the curve function 
>>>> it
>>>> confuses me.
>>>>
>>>>  curve(x^2)
>>>>> 
>>>> Works fine.
>>>>
>>>>  curve(x)
>>>>> 
>>>> Makes a complaint I don't understand.  Why is x^2 valid and x is not?
>>>> 
>>> 
>>> curve() is a convenience function, and it tries to guess what you mean.
>>>  Sometimes it gets it wrong.
>>> 
>>> In the first case, it is clear you want to graph x^2.  In the second it
>>> guesses you have a function named x and want to graph that.  You don't, so
>>> it fails.
>>> 
>>> Probably it could try again after the first failure, but I'd guess there
>>> will always be strange cases where it does weird things.
>>> 
>>> Duncan Murdoch
>>> 
>>> 
>>>> I check the documentation of curve, and it says the first argument must 
>>>> be
>>>> an expression containing x.
>>>>
>>>>  expression(x)
>>>>> 
>>>> Is an expression containing x.
>>>>
>>>>  curve(expression(x))
>>>>> 
>>>> Makes a different complaint and mentions different lengths of x and y 
>>>> (but
>>>> I
>>>> use no y here).
>>>> 
>>>> I understand that plotting the function y(x) = x is rather silly, but I
>>>> want
>>>> to know what I am doing wrong, for the sake of my understanding of how R
>>>> works.
>>>> 
>>>> Thank you for support.
>>>> Abhilash B.
>>>>
>>>>        [[alternative HTML version deleted]]
>>>> 
>>>> ______________________________________________
>>>> R-help at r-project.org mailing list
>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>> PLEASE do read the posting guide
>>>> http://www.R-project.org/posting-guide.html
>>>> and provide commented, minimal, self-contained, reproducible code.
>>>> 
>>> 
>>> 
>>
>> 	[[alternative HTML version deleted]]
>> 
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide 
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>> 
>
> -- 
> Brian D. Ripley,                  ripley at stats.ox.ac.uk
> Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
> University of Oxford,             Tel:  +44 1865 272861 (self)
> 1 South Parks Road,                     +44 1865 272866 (PA)
> Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595


More information about the R-help mailing list