# How to add non-linear trend line to a scatter plot in R?

I have a scatter plot. How can I add non-linear trend line?

-
Do you already have the equation of the trend curve or does adding it include computing its equation from the data? –  whuber Jun 23 '12 at 15:16

If you use ggplot2 (the third plotting system, in R, after base R and lattice), this becomes:

library(ggplot2)
ggplot(Data, aes(x,y)) + geom_point() + geom_smooth()


You can choose how the data is smoothed: see ?stat_smooth for details and examples.

-

Let's create some data.

n <- 100
x <- seq(n)
y <- rnorm(n, 50 + 30 * x^(-0.2), 1)
Data <- data.frame(x, y)


The following shows how you can fit a loess line or the fit of a non-linear regression.

plot(y ~ x, Data)

# fit a loess line
loess_fit <- loess(y ~ x, Data)
lines(Data$x, predict(loess_fit), col = "blue") # fit a non-linear regression nls_fit <- nls(y ~ a + b * x^(-c), Data, start = list(a = 80, b = 20, c = 0.2)) lines(Data$x, predict(nls_fit), col = "red")


-

Your question is a bit vague, so I'm going to make some assumptions about what your problem is. It would help a lot if you could put up a scatterplot and describe the data a bit. Please, if I'm making bad assumptions then ignore my answer.

First, it's possible that your data describe some process which you reasonably believe is non-linear. For instance, if you're trying to do regression on the distance for a car to stop with sudden braking vs the speed of the car, physics tells us that the energy of the vehicle is proportional to the square of the velocity - not the velocity itself. So you might want to try polynomial regression in this case, and (in R) you could do something like model <- lm(d ~ poly(v,2),data=dataset). There's a lot of documentation on how to get various non-linearities into the regression model.

On the other hand, if you've got a line which is "wobbly" and you don't know why it's wobbly, then a good starting point would probably be locally weighted regression, or loess in R. This does linear regression on a small region, as opposed to the whole dataset. It's easiest to imagine a "k nearest-neighbour" version, where to calculate the value of the curve at any point, you find the k points nearest to the point of interest, and average them. Loess is just like that but uses regression instead of a straight average. For this, use model <- loess(y ~ x, data=dataset, span=...), where the span variable controls the degree of smoothing.

On the third hand (running out of hands) - you're talking about trends? Is this a temporal problem? If it is, be a little cautious with over interpreting trend lines and statistical significance. Trends in time series can appear in "autoregressive" processes, and for these processes the randomness of the process can occasionally construct trends out of random noise, and the wrong statistical significance test can tell you it's significant when it's not!

-
Without knowing exactly what you are looking for, using the lattice package you can easily add a loess curve with type="smooth"; e.g.,
> library(lattice)

See help("panel.loess") for arguments that can be passed to the loess fitting routine in order to change, for instance, the degree of the polynomial to use.