Learn R Programming

ggpmisc (version 0.3.9)

position_nudge_center: Nudge labels away from a central point

Description

`position_nudge_center()` is generally useful for adjusting the position of labels or text, both on a discrete or continuous scale. In contrast to [ggplot2::position_nudge], `position_nudge_center()` returns in `data` both the original coordinates and the nudged coordinates.

Usage

position_nudge_center(
  x = 0,
  y = 0,
  center_x = NULL,
  center_y = NULL,
  direction = NULL,
  obey_grouping = NULL
)

position_nudge_centre( x = 0, y = 0, center_x = NULL, center_y = NULL, direction = NULL, obey_grouping = NULL )

position_nudge_keep(x = 0, y = 0)

Arguments

x, y

Amount of vertical and horizontal distance to move. A numeric vector of length 1, or of the same length as rows there are in `data`,

center_x, center_y

The coordinates of the virtual origin out from which nudging radiates or splits in opposite directions. A numeric vector of length 1 or of the same length as rows there are in `data`, or a function returning either of these vectors computed from the variables in data mapped to `x` or `y`, respectively.

direction

One of "none", "radial", or "split". A value of "none" replicates the behavior of [ggplot2::position_nudge]. Which of these three values is the default depends on the values passed to the other parameters.

obey_grouping

A logical flag indicating whether to obey or not groupings of the observations. By default, grouping is obeyed when both of the variables mapped to _x_ and _y_ are continuous numeric and ignored otherwise.

Details

This position function is backwards compatible with [ggplot2::position_nudge] but extends it by adding support for nudging that varies across the plotting region, either in opposite directions or radially from a virtual _center point_.

The wrapper `position_nudge_keep()` with exactly the same signature and behaviour as [ggplot2::position_nudge] provides an easier to remember name when the desire is only to have access to both the original and nudged coordinates.

Positive values as arguments to `x` and `y` are added to the original position along either axis. If no arguments are passed to `center_x`, `center_y` or `direction`, the nudging is applied as is, as is the case if `direction = "none"`. If non-`NULL` arguments are passed to both `center_x` and `center_y`, `direction = "radial"` is assumed. In this case, if `x` and/or `y` positive nudging is applied radially outwards from the center, while if negative, inwards towards the center. When a non-`NULL` argument is passed only to one of `center_x` or `center_y`, `direction = "split"` is assumed. In this case when the initial location of the point is to the left of `center_x`, `-x` is used instead of `x` for nudging, and when the initial location of the point is to the below of `center_y`, `-y` is used instead of `y` for nudging. If non-`NULL` arguments are passed to both `center_x` and `center_y`, and `direction` is passed `"split"` as argument, then the split as described above is applied to both _x_ and _y_ coordinates.

See Also

[ggplot2::position_nudge()], [ggrepel::position_nudge_repel()].

Other position adjustments: position_nudge_line(), position_nudge_to()

Examples

Run this code
# NOT RUN {
df <- data.frame(
  x = c(1,3,2,5,4,2.5),
  y = c("abc","cd","d","c","bcd","a")
)

# Plain nudging, same as with ggplot2::position_nudge()

ggplot(df, aes(x, y, label = y)) +
  geom_point() +
  geom_text(hjust = 0, vjust = 0,
            position = position_nudge(x = 0.05, y = 0.07)
  )

ggplot(df, aes(x, y, label = y)) +
  geom_point() +
  geom_text(hjust = 0, vjust = 0,
            position = position_nudge_center(x = 0.05, y = 0.07)
  )

# "split" nudging

ggplot(df, aes(x, y)) +
  geom_point() +
  geom_text(aes(label = y),
            hjust = "outward", vjust = "outward",
            position = position_nudge_center(x = 0.05,
                                             y = 0.07,
                                             direction = "split"))

ggplot(df, aes(x, y)) +
  geom_point() +
  geom_text(aes(label = y),
            hjust = "outward",
            position = position_nudge_center(x = 0.08,
                                             direction = "split"))

ggplot(df, aes(x, y)) +
  geom_point() +
  geom_text(aes(label = y),
            vjust = "outward",
            position = position_nudge_center(y = 0.1,
                                             direction = "split"))

ggplot(df, aes(x, y)) +
  geom_point() +
  geom_text(aes(label = y),
            vjust = "outward", hjust = "outward",
            position = position_nudge_center(x = 0.06,
                                             y = 0.08,
                                             center_y = 2,
                                             center_x = 1.5,
                                             direction = "split"))

ggplot(df, aes(x, y)) +
  geom_point() +
  geom_text(aes(label = y),
            vjust = "outward", hjust = "outward",
            position = position_nudge_center(x = 0.06,
                                             y = 0.08,
                                             center_y = 2))

ggplot(df, aes(x, y)) +
  geom_point() +
  geom_text(aes(label = y),
            vjust = "outward", hjust = "outward",
            position = position_nudge_center(x = 0.1,
                                             center_x = 2.5))

ggplot(df, aes(x, y)) +
  geom_point() +
  geom_text(aes(label = y),
            vjust = "outward", hjust = "outward",
            position = position_nudge_center(x = 0.06,
                                             y = 0.08,
                                             center_x = median,
                                             center_y = median,
                                             direction = "split"))

# "Radial" nudging

ggplot(df, aes(x, y)) +
  geom_point() +
  geom_text(aes(label = y),
            vjust = "outward", hjust = "outward",
            position = position_nudge_center(x = 0.1,
                                             y = 0.2,
                                             direction = "radial"))

ggplot(df, aes(x, y)) +
  geom_point() +
  geom_text(aes(label = y),
            vjust = "inward", hjust = "inward",
            position = position_nudge_center(x = -0.1,
                                             y = -0.1,
                                             direction = "radial"))

df <- data.frame(
  x = -10:10,
  z = (-10:10)^2,
  y = letters[1:21],
  group = rep(c("a", "b"), rep(c(11, 10)))
)

ggplot(df, aes(x, z)) +
  geom_point() +
  geom_line() +
  geom_text(aes(label = y),
            vjust = "inward", hjust = "inward",
            position = position_nudge_center(x = -0.9,
                                             y = -2.7,
                                             center_x = mean,
                                             center_y = max))

ggplot(df, aes(x, z)) +
  geom_point() +
  geom_line() +
  geom_text(aes(label = y),
            vjust = "outward", hjust = "outward",
            position = position_nudge_center(x = 0.9,
                                             y = 2.7,
                                             center_x = mean,
                                             center_y = max))

above_max <- function(x) {1.2 * max(x)}
ggplot(df, aes(x, z)) +
  geom_point() +
  geom_line() +
  geom_text(aes(label = y),
            vjust = "inward", hjust = "inward",
            position = position_nudge_center(x = -1.2,
                                             y = -3,
                                             center_x = mean,
                                             center_y = above_max))

ggplot(df, aes(x, z, color = group)) +
  geom_point() +
  geom_line(color = "black", linetype = "dotted") +
  geom_text(aes(label = y),
            vjust = "inward", hjust = "inward",
            position = position_nudge_center(x = -0.9,
                                             y = -2.7,
                                             center_x = mean,
                                             center_y = max))

ggplot(df, aes(x, z, color = group)) +
  geom_point() +
  geom_line(color = "black", linetype = "dotted") +
  geom_text(aes(label = y),
            vjust = "inward", hjust = "inward",
            position = position_nudge_center(x = -0.9,
                                             y = -2.7,
                                             center_x = mean,
                                             center_y = max,
                                             obey_grouping = FALSE))

# }

Run the code above in your browser using DataLab