how to add Horizontal Line to Plot and Legend in ggplot2

To add a horizontal line to a plot or legend in ggplot2, the geom_hline() function can be used. This function requires the user to specify the y-intercept of the line, as well as the aesthetics of the line, such as color and line type. The geom_hline() function can then be added to an existing ggplot2 plot with the + operator. The legend of the plot can then be modified with the guides() function.


You can use the following syntax to add a horizontal line to a plot in ggplot2 and then add the horizontal line as an element on the legend as well:

library(ggplot2)

#create data frame with values to plot
df <- data.frame(team=rep(c('A', 'B'), each=5),
                 assists=c(1, 3, 3, 4, 5, 7, 7, 9, 9, 10),
                 points=c(4, 8, 12, 10, 18, 25, 20, 28, 33, 35))

#create data frame that contains horizontal line location
cutoff <- data.frame(yintercept=22, Lines='Cutoff')

#create scatterplot with horizontal line and include horizontal line in legend
ggplot(df, aes(x=assists, y=points)) + 
  geom_point(aes(color=team)) +
  geom_hline(aes(yintercept=yintercept, linetype=Lines), cutoff)

By creating a separate data frame that only contains the value for the y-intercept of the horizontal line, we’re able to add the horizontal line to the plot and automatically add it to the legend as well.

The following example shows how to use this syntax in practice.

Example: Add Horizontal Line to Plot and Legend in ggplot2

Suppose we have the following data frame in R that contains information about basketball players on various teams:

#create data frame
df <- data.frame(team=rep(c('A', 'B'), each=5),
                 assists=c(1, 3, 3, 4, 5, 7, 7, 9, 9, 10),
                 points=c(4, 8, 12, 10, 18, 25, 20, 28, 33, 35))

#view data frame
df

   team assists points
1     A       1      4
2     A       3      8
3     A       3     12
4     A       4     10
5     A       5     18
6     B       7     25
7     B       7     20
8     B       9     28
9     B       9     33
10    B      10     35

Suppose we would like to create a scatter plot in ggplot2 to visualize the points and assists values for each player based on their team and then add a horizontal line at y=22 to define a “cutoff” for the difference between good and bad players.

We can use the following syntax to do so:

library(ggplot2)

#create data frame that contains horizontal line location
cutoff <- data.frame(yintercept=22, Lines='Cutoff')

#create scatterplot with horizontal line and include horizontal line in legend
ggplot(df, aes(x=assists, y=points)) + 
  geom_point(aes(color=team)) +
  geom_hline(aes(yintercept=yintercept, linetype=Lines), cutoff)

ggplot2 add horizontal line to legend

Notice that the legend to the right of the plot contains circles that show which points in the plot belong to which teams and a horizontal line has been added to the legend as well to represent the cutoff line.

If you’d like to change the legend for the horizontal line in the legend, simply change the text for the Lines column in the cutoff data frame.

For example, we can use the following syntax to change the label for the horizontal line to be “Cutoff of Good vs. Bad”:

library(ggplot2)

#create data frame that contains horizontal line location
cutoff <- data.frame(yintercept=22, Lines='Cutoff of Good vs. Bad')

#create scatterplot with horizontal line and include horizontal line in legend
ggplot(df, aes(x=assists, y=points)) + 
  geom_point(aes(color=team)) +
  geom_hline(aes(yintercept=yintercept, linetype=Lines), cutoff)

Notice that the label for the horizontal line in the legend has changed.

The following tutorials explain how to perform other common tasks in ggplot2:

x