position_stacknudge {ggpp} | R Documentation |
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 = c("none", "split", "split.x", "split.y"),
kept.origin = c("stacked", "original", "none")
)
position_fillnudge(
vjust = 1,
reverse = FALSE,
x = 0,
y = 0,
direction = c("none", "split", "split.x", "split.y"),
kept.origin = c("stacked", "original", "none")
)
position_stack_keep(vjust = 1, reverse = FALSE, kept.origin = "original")
position_fill_keep(vjust = 1, reverse = FALSE, kept.origin = "original")
position_stack_minmax(
vjust = 1,
reverse = FALSE,
x = 0,
y = 0,
direction = c("none", "split", "split.x", "split.y"),
kept.origin = c("stacked", "original", "none")
)
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 |
direction |
One of |
kept.origin |
One of |
Details
position_fillnudge()
is useful when labelling plots such as filled
bars, filled columns, filled lines, etc. In contrast to
position_nudge
, position_fillnudge()
returns
in data
both the original coordinates and the nudged coordinates.
The wrapper position_nudge_keep()
has the same signature and
behaviour as position_nudge
nad provides an easier to
remember name when the need is only to have access to both the original and
nudged coordinates.
These position functions are 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_stack_keep()
has the same signature and
behaviour as position_stack
and provides an easier to
remember name when the need is only to have access to both the original and
nudged coordinates.
The wrapper position_fill_keep()
has the same signature and
behaviour as position_fill
and provides an easier to
remember name when the need is only to have access to both the original and
nudged coordinates.
The wrapper position_stack_minmax()
has the same signature and
behaviour as position_stacknudge
but stacks y, ymin and
ymax in parallel, making it possible to stack summaries with error bars,
works correctly with geom_pointrange()
, geom_linerange()
and
geom_errorbar()
.
Value
A "Position"
object.
Author(s)
MichaĆ Krassowski, edited by Pedro J. Aphalo.
Source
https://github.com/slowkow/ggrepel/issues/161.
See Also
position_nudge
,
position_stack
,
position_nudge_repel
.
Other position adjustments:
position_dodgenudge()
,
position_jitternudge()
,
position_nudge_center()
,
position_nudge_keep()
,
position_nudge_line()
,
position_nudge_to()
Examples
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")
ggplot(birch_dw.df,
aes(y = dry.weight * 1e-3, x = Density, fill = Part)) +
stat_summary(geom = "col", fun = mean,
position = "stack", alpha = 0.7, width = 0.67) +
stat_summary(geom = "linerange", fun.data = mean_cl_normal,
position = position_stack_minmax()) +
labs(y = "Seedling dry mass (g)") +
scale_fill_grey(start = 0.7, end = 0.3) +
facet_wrap(facets = vars(Container))