Skip to content

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

3 messages · Tal Galili, Marc Schwartz, William Dunlap

#
On Nov 6, 2013, at 10:40 AM, Tal Galili <tal.galili at gmail.com> wrote:

            
Hi Tal,

In your example:

  plot(x ~ x)

the formula method of plot() is called, which essentially does the following internally:
x
1 1
2 2
3 9

Note that there is only a single column in the result. Thus, the plot is based upon 'y' = c(1, 2, 9), while 'x' = 1:3, which is NOT the row names for the resultant data frame, but the indices of the vector elements in the 'x' column. 

This is just like:

  plot(c(1, 2, 9))


On the other hand:
x c(x)
1 1    1
2 2    2
3 9    9
x I(x)
1 1    1
2 2    2
3 9    9


In both of the above cases, you get two columns of data back, thus the result is essentially:

  plot(c(1, 2, 9), c(1, 2, 9))


Regards,

Marc Schwartz
#
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