<- mgrtibbles::crab_age_pred_tbl |>
crab_age_weight_tbl ::slice(1:200) |>
dplyr::select(Weight, Age) dplyr
Annotation

Text and lines can be added to a plot as a form of annotation. This page will demonstrate how to:
- Add text annotation on top of a plot
- Add multiple annotations including boxes and lines
- Add horizontal & vertical lines
- Add a diagonal abline
- Add a linear model abline with the equation
Dataset
We’ll recreate many of the plots in the basic plot chapter, so we’ll load the crab_age_pred_tbl
data from the mgrtibbles package (hyperlink includes install instructions). Additionally, we’ll slice out the first 200 rows and select the columns we will utilise in the plot for a clear demonstration.
Annotation
Text annotation
Text can be placed anywhere on a plot.
To add text to a plot we can use the ggplot2::annotate()
component/function with the following options:
geom=
: This sets the type of geom to use for annotation. In this case it is “text” but others are demonstrated further below.x=
: The x position to place the annotation.y=
: The y position to place the annotation.label=
: Specifies the text.
::ggplot(data=crab_age_weight_tbl, mapping=aes(x=Age, y = Weight)) +
ggplot2#Set plot as a dot/scatter plot
::geom_point() +
ggplot2#Add text annotation
::annotate(geom="text", x = 22.5, y = 50, label = "Text annotation") ggplot2
If you want to plot points as text you can look at the geom_text()
function.
Multiple annotations
The below code demonstrates how to add multiple annotations and how to create rectangles (geom="rect"
) and lines (geom="segment"
).
Note: Alpha refers to the transparency of an object. 0 = completely transparent, 1= completely opaque/no transparency.
::ggplot(data=crab_age_weight_tbl, mapping=aes(x=Age, y = Weight)) +
ggplot2#Set plot as a dot/scatter plot
::geom_point() +
ggplot2#Add text annotation "Blue box"
::annotate(geom="text", x = 10.5, y = 58, label = "Red lined box", size = 7) +
ggplot2#Add annotation "Red lined box"
::annotate(geom="text", x = 16.5, y = 62, label = "Blue box", size = 7) +
ggplot2#Add a shaded rectangle with transparency and an outer line colour of red
::annotate(geom="rect", xmin = 8.5, xmax = 12.5,
ggplot2ymin=2, ymax=55, alpha=0.2, colour="red") +
#Add a shaded blue rectangle with transparency
::annotate(geom="rect", xmin = 14.5, xmax = 18.5,
ggplot2ymin=43, ymax=60, alpha=0.2, fill="blue") +
#Add a green line
::annotate("segment", x = 13, xend = 20, y = 10, yend=40, colour= "green" ) ggplot2
Reference lines
Three different types of reference lines can be added to a ggplot.
ggplot2::geom_abline()
: A diagonal line based on the slope and interceptggplot2::geom_hline()
: A horizontal line based on the y interceptggplot2::geom_vline()
: A vertical line based on the x intercept
Horizontal and vertical
Vertical and horizontal lines can be added with ggplot2::geom_vline()
and ggplot2::geom_hline()
.
The below code demonstrates adding both these lines.
::ggplot(data=crab_age_weight_tbl, mapping=aes(x=Age, y = Weight)) +
ggplot2#Set plot as a dot/scatter plot
::geom_point() +
ggplot2#Vertical lines
::geom_vline(xintercept=15) +
ggplot2#Horizontal lines
::geom_hline(yintercept=20) ggplot2
Abline
An abline is a straight diagonal line based on the intercept and slope.
Manual abline
An abline can be manually added with ggplot2::geom_abline()
by setting the intercept (intercept=
) and the slope (slope=
).
::ggplot(data=crab_age_weight_tbl, mapping=aes(x=Age, y = Weight)) +
ggplot2#Set plot as a dot/scatter plot
::geom_point() +
ggplot2#Abline
::geom_abline(intercept = 5, slope = 2) ggplot2
Statistical abline
An abline calculated with a linear model can be added with ggplot2::geom_smooth()
.
::ggplot(data=crab_age_weight_tbl, mapping=aes(x=Age, y = Weight)) +
ggplot2#Set plot as a dot/scatter plot
::geom_point() +
ggplot2#Linear model abline
::geom_smooth(method="lm") ggplot2
`geom_smooth()` using formula = 'y ~ x'
Statistical abline and equation
You can include the linear equation on the plot.
First we need to calculate the linear model with the base R stats package (preinstalled in R).
#Calculate and extract the slope and gradient
<- lm(Weight~Age, data = crab_age_weight_tbl)
lm_res #Intercept
<- coef(lm_res)[[1]]
c #Slope
<- coef(lm_res)[[2]]
m #R squared value
<- summary(lm_res)$adj.r.squared
r_squared #Equation
<- paste0("y = ", round(m, 3), "x + ", round(c, 3), ", R^2 = ", round(r_squared, 3))
lm_equation #Print lm equation
lm_equation
[1] "y = 2.351x + 0.527, R^2 = 0.285"
The equation can then be included with ggplot2::annotate()
.
::ggplot(data=crab_age_weight_tbl, mapping=aes(x=Age, y = Weight)) +
ggplot2#Set plot as a dot/scatter plot
::geom_point() +
ggplot2#Linear model abline
::geom_smooth(method="lm") +
ggplot2::annotate(geom="text", x = 7.5, y = 60, label = lm_equation) ggplot2
`geom_smooth()` using formula = 'y ~ x'