Table of Contents
Drawing arrows in ggplot2 is a useful tool for visualizing relationships and highlighting specific data points in a plot. To draw arrows in ggplot2, one can use the geom_curve() function and specify the x and y coordinates for the starting and ending points of the arrow. Additionally, the angle and curvature of the arrow can be adjusted to suit the specific needs of the plot. This feature allows for the creation of informative and visually appealing graphs in ggplot2.
Draw Arrows in ggplot2 (With Examples)
You can use the following basic syntax to draw an arrow in a plot in ggplot2:
library(ggplot2) ggplot(df, aes(x=x, y=y)) + geom_point() + geom_segment(aes(x=5, y=6, xend=8, yend=9), arrow = arrow(length=unit(0.5, 'cm')))
Here is what each argument does in the geom_segment() function:
- x: The x-value to start at
- y: The y-value to start at
- xend: The x-value to end at
- yend: The y-value to end at
- arrow: The length of the arrow head
The following example shows how to draw an arrow using ggplot2 in practice.
Example: Draw Arrows in ggplot2
Suppose we have the following data frame that contains information on the number of points scored and rebounds collected by various basketball players:
#create data frame df <- data.frame(points=c(3, 3, 5, 6, 7, 8, 9, 9, 8, 5), rebounds=c(2, 6, 5, 5, 8, 5, 9, 9, 8, 6)) #view data frame df points rebounds 1 3 2 2 3 6 3 5 5 4 6 5 5 7 8 6 8 5 7 9 9 8 9 9 9 8 8 10 5 6
We can use the following syntax to create a scatter plot in ggplot2 and add an arrow to specific location on the plot:
library(ggplot2) #create scatterplot and add arrow ggplot(df, aes(x=points, y=rebounds)) + geom_point() + geom_segment(aes(x=5, y=6, xend=8, yend=9), arrow = arrow(length=unit(.5, 'cm')))

Feel free to modify the value in the arrow() function to increase or decrease the size of the arrow head.
For example, the following code shows how to increase the size:
library(ggplot2) #create scatterplot and add arrow with increased arrow head size ggplot(df, aes(x=points, y=rebounds)) + geom_point() + geom_segment(aes(x=5, y=6, xend=8, yend=9), arrow = arrow(length=unit(2, 'cm')))

You can also use the color and lwd arguments to change the color and line width of the arrow, respectively:
library(ggplot2) #create scatterplot and add customized arrow ggplot(df, aes(x=points, y=rebounds)) + geom_point() + geom_segment(aes(x=5, y=6, xend=8, yend=9), arrow = arrow(length=unit(.5, 'cm')), color='red', lwd=3)

Feel free to play around with the various arguments in the geom_segment() function to create an arrow that looks exactly how you’d like.
The following tutorials explain how to perform other common tasks in ggplot2:
Cite this article
stats writer (2024). How can I draw arrows in ggplot2?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-draw-arrows-in-ggplot2/
stats writer. "How can I draw arrows in ggplot2?." PSYCHOLOGICAL SCALES, 25 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-draw-arrows-in-ggplot2/.
stats writer. "How can I draw arrows in ggplot2?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-draw-arrows-in-ggplot2/.
stats writer (2024) 'How can I draw arrows in ggplot2?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-draw-arrows-in-ggplot2/.
[1] stats writer, "How can I draw arrows in ggplot2?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I draw arrows in ggplot2?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
