Let's add a fit line to a scatterplot!

## Fit Line in Base Graphics

Here's how to do it in **base** graphics:

```
ols <- lm(Temp ~ Solar.R,
data = airquality)
summary(ols)
plot(Temp ~ Solar.R,
data = airquality)
abline(ols
```

## Fit Line in ggplot

And here's how to do it in **ggplot**:

```
library(ggplot2)
ggplot(data = airquality,
aes(Solar.R, Temp)) +
geom_point(pch = 19) +
geom_abline(intercept = ols$coefficients[1],
slope = ols$coefficients[2])
```

You can access the info from your regression results through `ols$coefficients`

.

Edit: Thanks to an anonymous commenter, I have learned that you can simplify this by using `geom_smooth`

. This way you don't have to specify the intercept and slope of the fit line.

```
ggplot(data = airquality,
aes(Solar.R, Temp)) +
geom_point(pch = 19) +
geom_smooth(method = lm,
se = FALSE)
```

## Fit Line in Lattice

In **lattice**, it's even easier. You don't even need to run a regression; you can just add to the `type`

option.

```
library(lattice)
xyplot(Temp ~ Solar.R,
data = airquality,
type = c("p", "r"))
```

The code is available in a gist.