R Error in xy.coords: x and y length differ (2 Examples)

In this R tutorial you’ll learn how to handle the Error in xy.coords that ‘x’ and ‘y’ lengths differ.

Creation of Example Data

my_x <- 5:8                           # Create example data
my_y <- 5:9

Example 1: Replicating the Error Message – ‘x’ and ‘y’ lengths differ

plot(my_x, my_y)                      # Plotting doesn't work due to length difference
# Error in xy.coords(x, y, xlabel, ylabel, log) : 
#   'x' and 'y' lengths differ

Example 2: Solving the Error Message – ‘x’ and ‘y’ lengths differ

my_y_short <- my_y[1:length(my_x)]    # Harmonize length of x and y
plot(my_x, my_y_short)                # Properly draw plot

r graph figure 1 r error xy coords x and y length differ

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed