[r] Plot a line graph, error in xy.coords(x, y, xlabel, ylabel, log) : 'x' and 'y' lengths differ

a<- c(2,2)
b<- c(3,4)
plot(a,b) # It works perfectly here

Then I tried:

t<-xy.coords(a,b)
plot(t) # It also works well here

Finally, I tried:

plot(t,1)

Now it shows me:

Error in xy.coords(x, y, xlabel, ylabel, log) : 'x' and 'y' lengths differ

It does not work, inside t, both a and b are of length 2, why it showing me x, y lengths differ?

This question is related to r plot

The answer is


plot(t) is in this case the same as

plot(t[[1]], t[[2]])

As the error message says, x and y differ in length and that is because you plot a list with length 4 against 1:

> length(t)
[1] 4
> length(1)
[1] 1

In your second example you plot a list with elements named x and y, both vectors of length 2, so plot plots these two vectors.

Edit:

If you want to plot lines use

plot(t, type="l")