Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

position_nudge_repel

Nudge labels a fixed distance from points


Description

position_nudge_repel is useful for adjusting the starting position of text labels before they are repelled from data points.

Usage

position_nudge_repel(x = 0, y = 0)

Arguments

x, y

Amount of horizontal and vertical distance to move. Same units as the data on the x and y axes.

Examples

df <- data.frame(
  x = c(1,3,2,5),
  y = c("a","c","d","c")
)

ggplot(df, aes(x, y)) +
  geom_point() +
  geom_text_repel(aes(label = y))

ggplot(df, aes(x, y)) +
  geom_point() +
  geom_text_repel(
    aes(label = y),
    min.segment.length = 0,
    position = position_nudge_repel(x = 0.1, y = 0.15)
  )

# The values for x and y can be vectors
ggplot(df, aes(x, y)) +
  geom_point() +
  geom_text_repel(
    aes(label = y),
    min.segment.length = 0,
    position = position_nudge_repel(
      x = c(0.1, 0, -0.1, 0),
      y = c(0.1, 0.2, -0.1, -0.2)
    )
  )

# We can also use geom_text_repel() with arguments nudge_x, nudge_y
ggplot(df, aes(x, y)) +
  geom_point() +
  geom_text_repel(
    aes(label = y),
    min.segment.length = 0,
    nudge_x = 0.1,
    nudge_y = 0.15
  )

# The arguments nudge_x, nudge_y also accept vectors
ggplot(df, aes(x, y)) +
  geom_point() +
  geom_text_repel(
    aes(label = y),
    min.segment.length = 0,
    nudge_x = c(0.1, 0, -0.1, 0),
    nudge_y = c(0.1, 0.2, -0.1, -0.2)
  )

ggrepel

Automatically Position Non-Overlapping Text Labels with 'ggplot2'

v0.9.1
GPL-3 | file LICENSE
Authors
Kamil Slowikowski [aut, cre] (<https://orcid.org/0000-0002-2843-6370>), Alicia Schep [ctb] (<https://orcid.org/0000-0002-3915-0618>), Sean Hughes [ctb] (<https://orcid.org/0000-0002-9409-9405>), Trung Kien Dang [ctb] (<https://orcid.org/0000-0001-7562-6495>), Saulius Lukauskas [ctb], Jean-Olivier Irisson [ctb] (<https://orcid.org/0000-0003-4920-3880>), Zhian N Kamvar [ctb] (<https://orcid.org/0000-0003-1458-7108>), Thompson Ryan [ctb] (<https://orcid.org/0000-0002-0450-8181>), Dervieux Christophe [ctb] (<https://orcid.org/0000-0003-4474-2498>), Yutani Hiroaki [ctb], Pierre Gramme [ctb], Amir Masoud Abdol [ctb], Malcolm Barrett [ctb] (<https://orcid.org/0000-0003-0299-5825>), Robrecht Cannoodt [ctb] (<https://orcid.org/0000-0003-3641-729X>), Michał Krassowski [ctb] (<https://orcid.org/0000-0002-9638-7785>), Michael Chirico [ctb] (<https://orcid.org/0000-0003-0787-087X>), Pedro Aphalo [ctb] (<https://orcid.org/0000-0003-3385-972X>)
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.