[R] Basic question: why does a scatter plot of a variable against itself works like this?

William Dunlap wdunlap at tibco.com
Wed Nov 6 17:59:09 CET 2013

It probably happens because plot(formula) makes one call to terms(formula) to
analyze the formula.  terms() says there is one variable in the formula,
the response, so plot(x~x) is the same a plot(seq_along(x), x).
If you give it plot(~x) , terms() also says there is one variable, but
no response, so you get the same plot as plot(x, rep(1,length(x))).
This is also the reason that plot(y1+y2 ~ x1+x2) makes one plot of the sum of y1 and y2
for each term on the right side instead of 4 plots, plot(x1,y1), plot(x1,y2),plot(x2,y1),
and plot(x2,y2).

One could write a plot function that called terms separately on the left and
right sides of the formula.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> Of Tal Galili
> Sent: Wednesday, November 06, 2013 8:40 AM
> To: r-help at r-project.org
> Subject: [R] Basic question: why does a scatter plot of a variable against itself works like
> this?
> Hello all,
> I just noticed the following behavior of plot:
> x <- c(1,2,9)
> plot(x ~ x) # this is just like doing:
> plot(x)
> # when maybe we would like it to give this:
> plot(x ~ c(x))
> # the same as:
> plot(x ~ I(x))
> I was wondering if there is some reason for this behavior.
> Thanks,
> Tal
> ----------------Contact
> Details:-------------------------------------------------------
> Contact me: Tal.Galili at gmail.com |
> Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) |
> www.r-statistics.com (English)
> ----------------------------------------------------------------------------------------------
> 	[[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.

More information about the R-help mailing list