# [Solved] Error In Xy.Coords(X, Y, Xlabel, Ylabel, Log) : ‘X’ And ‘Y’ Lengths Differ

The following is an example of a common R error: “Error In Xy.Coords(X, Y, Xlabel, Ylabel, Log) : ‘X’ And ‘Y’ Lengths Differ”

This error occurs when you try to create a plot of two variables that are not the same length.

This tutorial explains how to correct this error.

## How To Replicate This Error?

Assume we want to make a scatterplot in R using the following two variables:

#define x and y variables x <- c(2, 5, 5, 8) y <- c(22, 28, 32, 35, 40, 41) #attempt to create scatterplot of x vs. y plot(x, y) Error in xy.coords(x, y, xlabel, ylabel, log) : 'x' and 'y' lengths differ

- We get an error because x and y are not the same length.
- This can be verified by printing the length of each variable:

#print length of x length(x) [1] 4 #print length of y length(y) [1] 6 #check if length of x and y are equal length(x) == length(y) [1] FALSE

## How To Fix This Error?

The simplest way to correct this error is to ensure that both vectors are the same length:

#define x and y variables to have same length x <- c(2, 5, 5, 8, 9, 12) y <- c(22, 28, 32, 35, 40, 41) #confirm that x and y are the same length length(x) == length(y) [1] TRUE create scatterplot of x vs. y plot(x, y)

- If one vector is shorter than the other, you could choose to plot only the values up to the shorter vector’s length.
- For example, if vector x has four values and vector y has six, we can make a scatterplot using only the first four values of each vector:

#define x and y variables x <- c(2, 5, 5, 8) y <- c(22, 28, 32, 35, 40, 41) #create scatterplot of first 4 pairwise values of x vs. y plot(x, y[1:length(x)]

The scatterplot is created using only the first four values of each vector.

Also read:- [Fixed] Error: ‘\u’ Used Without Hex Digits In Character String Starting “”c:\u”

## Conclusion

This is a common error that occurs on R. We have tried to explain both the reason and the solution to this error in this guide, and we hope you found it helpful.

If you have any queries/suggestions regarding this error, please let us know in the comments below.