How To fix The R Error: XY coordinates: x y lengths differ

As a Rstudio user who frequently plots and graphs their datasets, you have probably come across this message at least once: Error in xy.coords(x, y, xlabel, ylabel, log). Let’s look at what this error is addressing; the xy coordinates function takes the values assigned the x and y coordinate to set the graphs file and column respectively. Now, one note on using this function with different libraries, make sure you know what your particular library reads for its coordinate function.
In this example, the data is being plotted against a new variable. When entered like so:
Vehicle_vol(VehicleData$Psgr_Vol + CommuterData$Lugg_Vol)
plot (CommuterData$Vehic_vol, VehicleData$CITY_SPEED)

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

The error message indicates an error in assigning the values to an x and y length. Sure enough, examining the commands reveals Vehic_vol is not recognized as a column in the VehicleData set. When assigning x and y values, the plot function aligns each column of the data set to its opposite, so when sets with unequal column numbers are paired, the function reads the one as being of a different length than the other and won’t graph them.

To solve this, the programmer needs to have the plot tool compare data sets with a matching number of columns. So to fix them:

VehicleData$Vehic_vol (VehicleData$Psgr_Vol + CommuterData$Lugg_Vol)
plot (VehicleData$Vehic_vol, VehicleData$CITY_MPG)

This plot includes sets with equal lengths and can be checked using the length[] function. When given the error message about xy, it would save time to verify that the lengths may be the root of the problem.

Let’s look at another example and see what else can spur this response. For this example, we’ll look at plotting a diagram with included error bars to pinpoint specific values on the visual. Each bar is a calculated mean or median value between an X and Y position, so there is a high potential to get the error message if the inputs are not implemented correctly. Let’s look at the code:
We’ll set the x column and the y values that will correspond on the plot;
x = 1:10:100
y = [20 30 45 40 60 65 80 75 95 90]
Notice that we’re being specific instead of simply putting the plot function for x and y. If we assigned a plot(x, y) function, we’d be assigning two sets of different lengths and the error message would follow. This way, we’re specifying what values we actually want to be compared, avoiding confusion. Now that we’re clear on x and y, let’s assign values for the error bars;
err = [5 8 2 9 3 3 8 3 9 3];
errorbar(x,y,err)
Notice that for the last command, we compare x to y and assign a bar to that point to outline it. We can assign the graph to not show a line for the curve and just have the values plotted with points contrasting x and y, as we get into more complex functionalities with R syntax.
Moving forward, these little tricks will allow greater use of the RStudio library in your regular data science uses. Look to expand on your skills by reading other users comments and trying some of their examples.

Scroll to top
Privacy Policy