I'm very new to R and I am having difficulty plotting a fitted curve. Ethanol.csv contains X,Y coordinates only.

Here is an example of the code I am using:

library(locfit)

ethanol_data <- read.csv(file = 'ethanol.csv', sep = ',')

x <- ethanol_data$X

y <- ethanol_data$Y

a1 <- locfit(y~lp(x, h=0.1) )

plot(x,y,col="white")

points(x,y, col="firebrick")

lines(x,fitted(a1), col="forestgreen", lwd=2,lty=1)

When I attempt to plot this with the lines function all the data points are connected to each other in a jumbled mess. I would like to plot lines connecting data points with only the nearest neighboring data points. I'm thinking that it is possible to sort the fitted coordinates based on one of the coordinates but I would like to know if there is an easier way to do it that doesn't require writing a new function. Any help/tips would be appreciated!