Extract datas from smooth curve in R

Hello everybody,

First of all, thanks for your help, and I hope I will understandable. I'm a french PhD student in human movement science, and I began with "R" 4 months ago. Then, I'm new on this website and I hope I ask the question in the correct section.

I give you my R code and I explain my problem :

# Show curve on a graphic with raw datas
plot(s9g_c_cor2$pupil_timestamp_secondes2 , s9g_c_cor2$diameter_3d , type='l' , xlab="secondes" , ylab="diamètre pupille" , col="royalblue3" , main="Evolution diamètre pupillaire")

# Show the smooth curve on graphic from raw datas
lines(smooth.spline(s9g_c_cor2$pupil_timestamp_secondes2 , s9g_c_cor2$diameter_3d),col="green",lwd=2)

This above code is correct (I can run it on R and it works, nevertheless I can't control the intensity of smoothing). However, now, I want to extract datas from the smooth curve. I tried differents code but it doesn't works (I give you, maybe, the code who is the most near to the good result, but maybe all is wrong and it's not the right way) :


smoothcurvedatas = ggplot(data = s9g_c_cor2, aes(x = pupil_timestamp_secondes2, y = diameter_3d)) +
  geom_point() +
  geom_smooth(span = 0)

To give an abstract, currently, I have raw datas in my first column of my dataframe "s9g_c_cor2" and now, I want to create in my dataframe a new column from datas smooth from the curve.

Thank you an other once for your help.

Yours sincerely,


your first approach involving base plotting, makes use of 'smooth.spline' , your second variation involving ggplot2 uses geom_smooth which would default to loess smoothing, though you try span=0 parameter, which I would expect to cause failures.

Do you care about the method of smoothing, or do you wish to get a smooth curve and data relating to it, without regard for a smoothing method ? (i.e I can pick one arbitrarily ?)

Thank you very much for your fast answer.

Yes, it's exactly that, I would like to get a smooth curve and data relating to it. I'm not really care about the method of smoothing (even if I would like understand the method). However if it's possible to control the intensity of smoothing it will be very good for me.

Yours sincerely,


You can adjust the degree of smoothing with span


(example_data <- mpg |> select(

#directly get a plot with smoothing , but we dont have the data ...
ggplot(example_data, aes(x = x, y = y)) +
  geom_point() +

# calculate the data and then plot that
myloess_smoother <- loess(y~x,

mydata2 <- example_data
mydata2$y_smoothed <- predict(myloess_smoother,newdata = mydata2)

ggplot(mydata2, aes(x = x, y = y)) +
  geom_point() +

Thank you very much !!! It works perfectly !!!

I worked on my datas, and I shaped my table and my code from your code. I think, what I did is not really beautiful but I success what I would to do and it's thanks to you so thank you very very much ! It's perfect !!!

Have a nice day,

Yours sincerely,


1 Like

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.