Drawing Arrows in ggplot2



This post explains how to draw arrows in ggplot2 with R. It covers three different methods: drawing the simplest arrow possible, customizing arrow style, and creating curved arrows. We’ll provide reproducible code and explain how it works for beginners.

ggplot2 Data To Viz

Libraries and dataset


We’ll be using the ggplot2 package to create our plots and draw arrows. Let’s start by loading the necessary libraries and creating a simple dataset:

# Load libraries
library(ggplot2)
library(dplyr)

# Create a simple dataset
set.seed(123)
df <- data.frame(
  x = 1:10,
  y = rnorm(10, mean = 5, sd = 2)
)

Drawing the Simplest Arrow Possible


The simplest way to draw an arrow in ggplot2 is by using the geom_segment() function with the arrow parameter.

In this code:

  1. We start with a basic scatter plot using geom_point().
  2. We add geom_segment() to draw the arrow.
  3. Inside geom_segment(), we specify the start (x, y) and end (xend, yend) coordinates of the arrow.
  4. The arrow() function is used to add an arrowhead to the end of the segment.
ggplot(df, aes(x, y)) +
  geom_point() +
  geom_segment(aes(x = 2, y = 2, xend = 8, yend = 8),
    arrow = arrow()
  ) +
  theme_minimal()

Customizing Arrow Style


We can customize various aspects of the arrow, such as its color, size, type, and the properties of the arrowhead.

  1. We set the length of the arrowhead to 0.5 cm using unit(0.5, "cm").
  2. We change the arrowhead type to “closed” for a filled arrowhead.
  3. We set the color of the arrow to red.
  4. We increase the size (thickness) of the arrow to 1.5.
  5. We change the linetype to “dashed”.
ggplot(df, aes(x, y)) +
  geom_point() +
  geom_segment(aes(x = 2, y = 2, xend = 8, yend = 8),
    arrow = arrow(length = unit(0.5, "cm"), type = "closed"),
    color = "red",
    size = 1.5,
    linetype = "dashed"
  ) +
  theme_minimal()

Drawing Curved Arrows


To draw curved arrows, we can use geom_curve() instead of geom_segment().

  1. We replace geom_segment() with geom_curve().
  2. We add a curvature parameter to control the bend of the arrow. Negative values curve the arrow clockwise, while positive values curve it counterclockwise.
  3. We adjust the length of the arrowhead to 0.3 cm for better proportions with the curve.
  4. We set the arrow color to blue and its size to 1.2.
ggplot(df, aes(x, y)) +
  geom_point() +
  geom_curve(aes(x = 2, y = 2, xend = 8, yend = 8),
    arrow = arrow(length = unit(0.3, "cm"), type = "closed"),
    color = "blue",
    size = 1.2,
    curvature = -0.3
  ) +
  theme_minimal()

Going further


You might be interested in:

Related chart types


Ggplot2
Animation
Interactivity
3D
Caveats
Data art



❤️ 10 best R tricks ❤️

👋 After crafting hundreds of R charts over 12 years, I've distilled my top 10 tips and tricks. Receive them via email! One insight per day for the next 10 days! 🔥