position_dodgenudge {ggpp} | R Documentation |
Combined positions dodge and nudge
Description
position_dodgenudge()
combines into one function the action of
position_dodge
and
position_nudge
and position_dodge2nudge()
combines into one function the action of
position_dodge2
and
position_nudge
. They are useful when labelling plots
such as grouped bars, columns, etc. and when adding dodged to text labels
linked to observations plotted without dodge. It can replace other position
functions as it is backwards compatible. Like all other position functions in
'ggpp' and 'ggrepel' it preserves the initial position to allow drawing of
segments or arrow linking the original position to the displaced one.
Usage
position_dodgenudge(
width = 1,
preserve = c("total", "single"),
x = 0,
y = 0,
direction = c("none", "split", "split.x", "split.y", "center"),
kept.origin = c("dodged", "original", "none")
)
position_dodge_keep(
width = 1,
preserve = c("total", "single"),
kept.origin = "original"
)
position_dodge2_keep(
width = 1,
preserve = c("total", "single"),
kept.origin = "original"
)
position_dodge2nudge(
width = 1,
preserve = c("total", "single"),
padding = 0.1,
reverse = FALSE,
x = 0,
y = 0,
direction = c("none", "split", "split.x", "split.y", "center"),
kept.origin = c("dodged", "original", "none")
)
Arguments
width |
Dodging width, when different to the width of the individual elements. This is useful when you want to align narrow geoms with wider geoms. See the examples. |
preserve |
Should dodging preserve the total width of all elements at a position, or the width of a single element?. |
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', with nudge values in data rows order. |
direction |
One of |
kept.origin |
One of |
padding |
Padding between elements at the same position. Elements are shrunk by this proportion to allow space between them. Defaults to 0.1. |
reverse |
If TRUE, will reverse the default stacking order. This is useful if you're rotating both the plot and legend. |
Details
The applied dodge is identical to that by
position_dodge
or
position_dodge2
while nudging is similar to that by
position_nudge
.
There are two posible uses for these functions. First they can be used to
label dodged bars or boxplots. In this case, it is mandatory to use the same
argument to width
when passing position_dodge()
to
geom_col()
and position_dodgenudge()
to geom_text()
or
geom_label()
or their repulsive equivalents. Otherwise the arrows or
segments will fail to connect to the labels. In other words jittering is
computed twice. Jitter should be identical with the same arguments as
position_dodgenudge()
as this last function simply call the same code
from package 'ggplot2'.
The second use is to dodge labels to be connected to elements that have not
been jittered. The return of original positions instead of the dodged
ones is achieved by passing origin = "original"
instead of the default
of origin = "dodged"
.
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_nudge_repel
.
Other position adjustments:
position_jitternudge()
,
position_nudge_center()
,
position_nudge_keep()
,
position_nudge_line()
,
position_nudge_to()
,
position_stacknudge()
Examples
df <- data.frame(x1 = c(1, 2, 1, 3, -1),
x2 = c("a", "a", "b", "b", "b"),
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.8,
position = position_dodge()) +
geom_vline(xintercept = 0) +
geom_text(
aes(label = grp),
position = position_dodgenudge(x = 0.09, direction = "split", width = 0.8),
angle = 90, size = 3) +
theme(legend.position = "none")
ggplot(data = df, aes(x2, x1, group = grp)) +
geom_col(aes(fill = grp), width = 0.75,
position = position_dodge(width = 0.75)) +
geom_vline(xintercept = 0) +
geom_text(aes(label = grp),
position = position_dodgenudge(y = 0.1,
direction = "split",
width = 0.75),
size = 3) +
theme(legend.position = "none")