Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

plot

r

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?

like image 330
ToBeGeek Avatar asked Nov 17 '13 02:11

ToBeGeek


People also ask

What does error in XY Coords X Y Xlabel Ylabel log x and y lengths differ?

coords(x, y, xlabel, ylabel, log) : 'x' and 'y' lengths differ. This error occurs when you attempt to create a plot of two variables but the variables don't have the same length.

What does it mean in R when it says X and Y lengths differ?

coords that 'x' and 'y' lengths differ to the RStudio console. The reason for this is that the two input vectors we have inserted in the plot function have a different length.


1 Answers

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")
like image 133
user1981275 Avatar answered Oct 22 '22 23:10

user1981275