# Adding a regression line on a ggplot

###### Posted By: Anonymous

I’m trying hard to add a regression line on a ggplot. I first tried with abline but I didn’t manage to make it work. Then I tried this…

```
data = data.frame(x.plot=rep(seq(1,5),10),y.plot=rnorm(50))
ggplot(data,aes(x.plot,y.plot))+stat_summary(fun.data=mean_cl_normal) +
geom_smooth(method='lm',formula=data$y.plot~data$x.plot)
```

But it is not working either.

## Solution

In general, to provide your own formula you should use arguments `x`

and `y`

that will correspond to values you provided in `ggplot()`

– in this case `x`

will be interpreted as `x.plot`

and `y`

as `y.plot`

. You can find more information about smoothing methods and formula via the help page of function `stat_smooth()`

as it is the default stat used by `geom_smooth()`

.

```
ggplot(data,aes(x.plot, y.plot)) +
stat_summary(fun.data=mean_cl_normal) +
geom_smooth(method='lm', formula= y~x)
```

If you are using the same x and y values that you supplied in the `ggplot()`

call and need to plot the linear regression line then you don’t need to use the formula inside `geom_smooth()`

, just supply the `method="lm"`

.

```
ggplot(data,aes(x.plot, y.plot)) +
stat_summary(fun.data= mean_cl_normal) +
geom_smooth(method='lm')
```

###### Answered By: Anonymous

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.