Error in xy.coords(x, y) : 'x' and 'y' lengths differ help

Hi, so I am a new Rstudio user and I am trying to make a line graph with multiple lines. I followed a tutorial online verbatim and I am getting an error message that says "Error in xy.coords(x, y) : 'x' and 'y' lengths differ".
The data I am running can be found here:

Coyote_Mean_Tracks<- c(2.70, 4.21, 7.73, 18.53, 20.30, 3.90, 0.59, 6.93, 17.41, 33.58, 39.43, 34.06, 41.68, 2.46, 13.14, 5.97, 4.45, 6.10, 9.80, 15.96, 19.08, 7.91, 8.53, 1.58, 2.70, 6.17, 10.00, 34.59, 19.14, 16.18, 15.43)
Lynx_Mean_Tracks <- c(7.92, 13.85, 22.40, 53.89, 35.80, 13.78, 4.57, 10.52, 14.79, 27.25, 53.87, 82.41, 72.68, 2.62, 2.89, 5.36, 6.38, 11.43, 21.59, 39.36, 16.24, 21.34, 21.98, 15.97, 14.31, 18.72, 16.50, 38.34, 30.97, 59.87, 59.67)
Mustelid_Mean_Tracks <- c(3.15, 4.02, 2.22, 3.21, 8.03, 12.16, 7.61, 28.61, 9.78, 11.44, 4.90, 16.31, 17.22, 9.85, 29.89, 67.88, 30.98, 21.35, 13.12, 66.70, 52.65, 49.20, 123.48, 84.21, 29.87, 58.08, 45.00, 78.85, 13.22, 6.77, 13.37)
Season <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31)

I ran:

plot(Season, Coyote_Mean_Tracks, type="o", col="blue", pch="o", lty=1, ylim=c(0,130))

and that created the first line perfectly.

BUT when I typed in this:

points(Season, Lynx_Mean_Tracks, col="red", pch="*") %>%
+lines(Season, Lynx_Mean_Tracks, col="red",lty=2)
Error in xy.coords(x, y) : 'x' and 'y' lengths differ

as well as the third line:

points(Season, Mustelid_Mean_Tracks, col="green",pch="+") %>%
+lines(Season, Mustelid_Mean_Tracks, col="green", lty=3)
Error in xy.coords(x, y) : 'x' and 'y' lengths differ

Help????

Hi, you try to create a plot in base R but you can't use a pipe in this case. Just add points() and lines() consecutively after you have created the plot:

plot(Season, Coyote_Mean_Tracks, type="o", col="blue", pch="o", lty=1, ylim=c(0,130))
points(Season, Lynx_Mean_Tracks, col="red", pch="*")
lines(Season, Lynx_Mean_Tracks, col="red",lty=2)

Cheers!

2 Likes

So one last question, how do I change my axes on my graph? I want the x axis seasons to be years instead of numbers, and I want the y axis "Coyote_Mean_Tracks" to be "Mean Tracks"

I am not an expert of base plotting but as far as I know you need to transform the values, i.e. by dividing seasons by 4. You can change the labels within the plot() call:

plot(Season / 4, Coyote_Mean_Tracks, type="o", col="blue", pch="o", lty=1, ylim=c(0,130), xlab = "Year", ylab = "Mean Tracks")
points(Season / 4, Lynx_Mean_Tracks, col="red", pch="*")
lines(Season / 4, Lynx_Mean_Tracks, col="red",lty=2)

I probably didn't explain that in the best way. Our seasons are years so our data set is: 1987/88, 88/89, 89/90, 90/91......17/18 instead of 1,2,3,4.

So just transform the numbers accordingly? 4 was just a guess based on the usual use of "season".

plot(Season + 1986, Coyote_Mean_Tracks, type="o", col="blue", pch="o", lty=1, ylim=c(0,130), xlab = "Year", ylab = "Mean Tracks")
points(Season + 1986, Lynx_Mean_Tracks, col="red", pch="*")
lines(Season + 1986, Lynx_Mean_Tracks, col="red",lty=2)

As I said I rarely use base plots but by googling I found this solution in a sec which should solve your problem in case you want both years for each label:

In general, these are very basic changes which are covered in most tutorials :slight_smile:
https://rstudio-pubs-static.s3.amazonaws.com/84527_6b8334fd3d9348579681b24d156e7e9d.html

Cheers!

Perfect! Thank you so much for your help!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.