# Annotating Plots

Want to learn more? I recommend working through: R for Data Science, R Cookbook, and R Graphics Cookbook.

```# load the gcookbook package for the data
library(gcookbook)

library(ggplot2)

# reset the graphing device
dev.off()
```
```quartz_off_screen
3
```
```# create the ggplot2 data
p <- ggplot(faithful, aes(x = eruptions, y = waiting))
```

```# create the ggplot2 plot
p + geom_point() +
annotate("text", x = 3, y = 48, label="Group 1", family="serif", fontface="italic", colour="darkred", size=6) +
annotate("text", x = 4.5, y = 66, label="Group 2", family="serif", fontface="italic", colour="darkred", size=6)

# create the ggplot2 plot
p + geom_point() +
# add the formula, parse=TRUE turns the next into a formula
annotate("text", x = 4.5, y = 66, parse = TRUE, label = "frac(1, sqrt(2 * pi)) * e ^ {-x^2 / 2}")
```

```# create the ggplot2 plot
p + geom_point() +
# add the formula, parse=TRUE turns the next into a formula
annotate("text", x = 4.5, y = 66, parse = TRUE, label = "frac(1, sqrt(2 * pi)) * e ^ {-x^2 / 2}")
```

```# load the grid package to create the flat ends of the line seqment and arrow
library(grid)

# create the ggplot2 plot
p + geom_point() +
# add a horizontal line at y = 66
geom_hline(yintercept = 66) +
# add a vertical line at 3 = 3
geom_vline(xintercept = 3) +
geom_abline(intercept = 37.4, slope = 9) +
```# create the ggplot2 plot