How to Use geom_segment() in R


You can use the geom_segment() function in ggplot2 to draw a straight line between specific points on a ggplot2 plot.

This function uses the following basic syntax:

p +
  geom_segment(x=70, y=30, xend=95, yend=35)

This particular example assumes that a ggplot object has been created and assigned to the variable named p.

We then use the geom_segment() function with the following arguments:

  • x: Starting x position
  • y: Starting y position
  • xend: Ending x position
  • yend: Ending y position

In this particular example, a straight line will be drawn from the (x, y) coordinate of (70, 30) to (95, 35) in the plot.

The following example shows how to use the geom_segment() function in practice in R.

Example: How to Use geom_segment() in R

Suppose that we create the following data frame in R that contains information about points and assists for various basketball teams:

#create data frame
df <- data.frame(points=c(99, 68, 86, 88, 95, 74, 78, 93),
                 assists=c(22, 28, 31, 35, 34, 45, 28, 31))

#view data frame
df

  points assists
1     99      22
2     68      28
3     86      31
4     88      35
5     95      34
6     74      45
7     78      28
8     93      31

Suppose that we would like to create a scatter plot to visualize the relationship between the values in the points and assists columns of the data frame.

We can use the geom_point() function from ggplot2 to do so.

If you don’t have the ggplot2 data visualization package already installed then you can use the following syntax to do so:

#install ggplot2 package if necessary
install.packages('ggplot2')

Once the package is installed, you can use the following syntax to create the scatter plot:

library(ggplot2)

#create scatter plot of points vs assists
ggplot(df, aes(x=points, y=assists)) +
  geom_point()

This produces the following plot:

The x-axis represents the points scored by each team and the y-axis represents the total assists for each team.

Suppose that we would like to use the geom_segment() function to draw a straight line ranging from (70, 30) to (95, 35) on the plot.

We can use the following syntax to do so:

library(ggplot2)

#create scatter plot of points vs assists
ggplot(df, aes(x=points, y=assists)) +
  geom_point() +
  geom_segment(x=70, y=30, xend=95, yend=35)

This produces the following plot:

Notice that a straight line has been added ranging from the exact coordinates that we specified.

Note that we can use the color argument to change the color of the line shown by the segment and the size argument to change the thickness of the line.

For example, we can use the following syntax to use a red line with a thickness value of 3:

library(ggplot2)

#create scatter plot of points vs assists
ggplot(df, aes(x=points, y=assists)) +
  geom_point() +
  geom_segment(x=70, y=30, xend=95, yend=35, color='red', size=3)

This produces the following plot:

geom_segment with specific size and color in R

Notice that the line is now red and much thicker than the line shown in the previous example.

Feel free to modify the values for the color and size arguments to create the exact straight line that you would like on your own plot.

Note: You can find the complete documentation for the geom_segment() function in ggplot2 here.

Additional Resources

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

How to Use geom_text() in ggplot2
How to Use scale_y_continuous in ggplot2
How to Rotate Axis Labels in ggplot2
How to Change Legend Labels in ggplot2

Featured Posts

Leave a Reply

Your email address will not be published. Required fields are marked *