Fixing R error: xy.coords(x, y, xlabel, ylabel, log) :’x’ and ‘y’ lengths differ

When doing data analysis in the r language, plotting the information you are working with is often helpful. You will get this error message when plotting the data from vectors with a different length. For the plotting function to work you need to use a numeric vector pair with the same lengths. Otherwise, the mismatch will cause a problem triggering our error message. Fixing the problem requires making sure that the vectors have the same lengths.

Description of the R Error

The plot function is a base r function and not part of an add-on package. This is not a problem that you will encounter when using an array or a data frame as the argument. You will not run into it when dealing with a column pair or pair of rows from the same data frame, because they all have the same lengths. It only occurs when using vectors with different lengths. Each element of these vectors needs to have a numeric value to be plotted, that value can be a positive integer, a negative integer, or any real number. The x and y label of the coordinates is irrelevant as is the scale of the output graph. However, when plotting data in statistical computing there can be no length variation in the coordinate lists.

Explanation of the R Error

Here we have an r code example of how to cause this problem and trigger our error message.

> x = c(1, 2, 3, 4, 5, 6, 7, 8)
> y = c(1, 2, 3, 4, 5, 6, 7, 8, 9)
> plot(x,y)
Error in xy.coords(x, y, xlabel, ylabel, log) :
‘x’ and ‘y’ lengths differ
> length(x) == length(y)
[1] FALSE

In this code, we use two vectors of different lengths. As shown in the logical comparison between the ylength and xlength they are not the same. You will note that the error message is triggered.

How to fix this R Error

In this tutorial, we have three r code Examples of how to fix this problem. The last one has two code examples illustrating the two possible outcomes.

> x = c(1, 2, 3, 4, 5, 6, 7, 8)
> y = c(1, 2, 3, 4, 5, 6, 7, 8)
> plot(x,y)

In this example, we simply remove the extra value from the longer vector. The result is that it produced the graph as expected.

> x = c(1, 2, 3, 4, 5, 6, 7, 8, NA)
> y = c(1, 2, 3, 4, 5, 6, 7, 8, 9)
> plot(x,y)

In this example, we simply add a missing value to the shorter vector. The result is that it produced the graph as expected.

> x = c(1, 2, 3, 4, 5, 6, 7, 8)
> y = c(1, 2, 3, 4, 5, 6, 7, 8, 9)
> if(length(x) == length(y)) plot(x,y)

In this example, we put the original vectors through an if statement comparing their lengths and the result is that no graph is produced because the graphing function is skipped avoiding the problem. This is a handy way of dealing with this error when you have no control over the data.

> x = c(1, 2, 3, 4, 5, 6, 7, 8)
> y = c(1, 2, 3, 4, 5, 6, 7, 8)
> if(length(x) == length(y)) plot(x,y)

In this example, we put the vectors with the same lengths through an if statement comparing their lengths. The result is that it produced the graph as expected because the plotting function is triggered when the vector lengths are the same. This is the most realistic way of fixing this problem because you often do not have any control over the content of the data you are working with, and in most cases, you would not want to tamper with it if you did. This approach makes for a straightforward way to avoid this problem when you do not have the control or knowledge necessary to correct it the other ways.

This is an easy error message to understand, but fixing it is a little tricky because there is more than one way that it can be done. However, once you understand all three ways of fixing this problem, it is a simple matter of deciding which one is best for your situation.

Scroll to top
Privacy Policy