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?
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.
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.
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With