Learn R Programming

ggpp (version 0.5.1)

position_stacknudge: Combined positions stack and nudge

Description

position_stacknudge() is useful when labelling plots such as stacked bars, stacked columns, stacked lines, etc. In contrast to position_nudge, position_stacknudge() returns in data both the original coordinates and the nudged coordinates.

Usage

position_stacknudge(
  vjust = 1,
  reverse = FALSE,
  x = 0,
  y = 0,
  direction = "none",
  kept.origin = "stacked"
)

position_fillnudge( vjust = 1, reverse = FALSE, x = 0, y = 0, direction = "none", kept.origin = "stacked" )

position_stack_keep(vjust = 1, reverse = FALSE)

position_fill_keep(vjust = 1, reverse = FALSE)

Value

A "Position" object.

Arguments

vjust

Vertical adjustment for geoms that have a position (like points or lines), not a dimension (like bars or areas). Set to 0 to align with the bottom, 0.5 for the middle, and 1 (the default) for the top.

reverse

If TRUE, will reverse the default stacking order. This is useful if you're rotating both the plot and legend.

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,

direction

One of "none", "split", "split.x" or "split.y". A value of "none" replicates the behavior of position_nudge. At the moment "split" changes the sign of the nudge at zero, which is suitable for column plots with negative slices.

kept.origin

One of "original", "stacked" or "none".

Author

Michał Krassowski, edited by Pedro J. Aphalo.

Details

This position function is backwards compatible with position_nudge but extends it by adding support for stacking and for geometries that make use of the original position to draw connecting segments or arrows.

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

See Also

position_nudge, position_stack, position_nudge_repel.

Other position adjustments: position_dodgenudge(), position_jitternudge(), position_nudge_center(), position_nudge_line(), position_nudge_to()

Examples

Run this code

df <- data.frame(x1 = c("a", "a", "b", "b", "b"),
                 x2 = c(1, 2, 1, 3, -1),
                 grp = c("some long name", "other name", "some name",
                         "another name", "some long name"))

# Add labels to a horizontal column plot (stacked by default)
ggplot(data = df, aes(x1, x2, group = grp)) +
  geom_col(aes(fill = grp), width=0.5) +
  geom_vline(xintercept = 0) +
  geom_text(
    aes(label = grp),
    position = position_stacknudge(vjust = 0.5, y = 0.3)) +
  theme(legend.position = "none")

# Add labels to a vertical column plot (stacked by default)
ggplot(data = df, aes(x2, x1, group = grp)) +
  geom_col(aes(fill = grp), width=0.5) +
  geom_vline(xintercept = 0) +
  geom_text(
    aes(label = grp),
    position = position_stacknudge(vjust = 0.5, x = -0.3),
    angle = 90) +
  theme(legend.position = "none")

# Add labels to a vertical column plot (stacked by default)
ggplot(data = subset(df, x1 >= 0), aes(x1, x2, group = grp)) +
  geom_col(aes(fill = grp), width=0.5, position = position_fill()) +
  geom_vline(xintercept = 0) +
  geom_text(
    aes(label = grp),
    position = position_fillnudge(vjust = 0.5, x = -0.3),
    angle = 90) +
  theme(legend.position = "none")

# Add label at a fixed distance from the top of each column slice
ggplot(data = df, aes(x1, x2, group = grp)) +
  geom_col(aes(fill = grp), width=0.5) +
  geom_vline(xintercept = 0) +
  geom_text(
    aes(label = grp),
    position = position_stacknudge(vjust = 1, y = -0.2)) +
  theme(legend.position = "none")

# Use geom_text_s(), geom_text_repel() or geom_label_repel() to link
# label to labelled segment or object with an arrow
ggplot(data = df, aes(x2, x1, group = grp)) +
  geom_col(aes(fill = grp), width=0.5) +
  geom_vline(xintercept = 0) +
  geom_text_s(
    aes(label = grp),
    position = position_stacknudge(vjust = 0.5, y = 0.35),
    vjust = "bottom") +
  theme(legend.position = "none")

Run the code above in your browser using DataLab