position_nudge: Nudge points a fixed distance

View source: R/position-nudge.R

position_nudgeR Documentation

Nudge points a fixed distance

Description

position_nudge() is generally useful for adjusting the position of items on discrete scales by a small amount. Nudging is built in to geom_text() because it's so useful for moving labels a small distance from what they're labelling.

Usage

position_nudge(x = NULL, y = NULL)

Arguments

x, y

Amount of vertical and horizontal distance to move.

Aesthetics

position_nudge() understands the following aesthetics. Required aesthetics are displayed in bold and defaults are displayed for optional aesthetics:

nudge_x 0
nudge_y 0

Learn more about setting these aesthetics in vignette("ggplot2-specs").

See Also

Other position adjustments: position_dodge(), position_identity(), position_jitter(), position_jitterdodge(), position_stack()

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(aes(label = y))

ggplot(df, aes(x, y)) +
  geom_point() +
  geom_text(aes(label = y), position = position_nudge(y = -0.1))

# Or, in brief
ggplot(df, aes(x, y)) +
  geom_point() +
  geom_text(aes(label = y), nudge_y = -0.1)

# For each text individually
ggplot(df, aes(x, y)) +
  geom_point() +
  geom_text(aes(label = y, nudge_y = c(-0.1, 0.1, -0.1, 0.1)))

tidyverse/ggplot2 documentation built on April 13, 2025, 11:34 a.m.