Stephen Clark
Stephen Clark

Reputation: 596

Add a median "best fit line" to a scatter plot in r

I would like to add an empirical line of best fit to a scatter plot that is based on the median y for a range of x, eg at x=0.105 would be the y line of the median y's for x in the range 0.100 to 0.110, at x=0.115 would be the y line of the median y's for x in the range 0.110 to 0.120, etc. These y line points will be joined up. These x's are just for illustration, I'd like to be flexible in their choice. (I do not want a parametric fit, thanks)

This simulates some data:

x<-0.1+runif(n=1000,min=0,max=1)
y<-1/x+runif(n=1000,min=0,max=1)*(1-x)*5
plot(x,y)

Here is a mock up of the median line in orange.

mock up of desired plot

Upvotes: 1

Views: 1893

Answers (2)

Mike
Mike

Reputation: 4370

I would look into the lowess smoothing as option. Look into the f option on how 'smooth' you would like it to be. I copied some documentation below.

option f the smoother span. This gives the proportion of points in the plot which influence the smooth at each value. Larger values give more smoothness.

x<-0.1+runif(n=1000,min=0,max=1)
    y<-1/x+runif(n=1000,min=0,max=1)*(1-x)*5    
    plot(x, y)
    lines(lowess(x, y), col=2)
    lines(lowess(x, y, f=.2), col=3)

Upvotes: 2

Cristian E. Nuno
Cristian E. Nuno

Reputation: 2920

After your plot() function, try the following:

lines( x = lowess( x = x, y = y ), col = "darkgoldenrod2", lwd = 4 )

From ?lowess:

lowess returns a list containing components x and y which give the coordinates of the smooth. The smooth can be added to a plot of the original points with the function lines().

Upvotes: 1

Related Questions