-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathposition_jitternudge.Rd
173 lines (155 loc) · 7.06 KB
/
position_jitternudge.Rd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/position-nudge-jitter.R
\name{position_jitternudge}
\alias{position_jitternudge}
\alias{position_jitter_keep}
\title{Combined positions jitter and nudge}
\source{
\url{https://github.com/slowkow/ggrepel/issues/161}.
}
\usage{
position_jitternudge(
width = NULL,
height = NULL,
seed = NA,
x = 0,
y = 0,
direction = c("as.is", "alternate", "split"),
nudge.from = c("original", "original.x", "original.y", "jittered", "jittered.y",
"jittered.x"),
kept.origin = c("jittered", "original", "none")
)
position_jitter_keep(width = NULL, height = NULL, seed = NA)
}
\arguments{
\item{width, height}{Amount of vertical and horizontal jitter. The jitter is
added in both positive and negative directions, so the total spread is
twice the value specified here. If omitted, defaults to 40% of the
resolution of the data: this means the jitter values will occupy 80% of the
implied bins. Categorical data is aligned on the integers, so a width or
height of 0.5 will spread the data so it's not possible to see the
distinction between the categories.}
\item{seed}{A random seed to make the jitter reproducible. Useful if you need
to apply the same jitter twice, e.g., for a point and a corresponding
label. The random seed is reset after jittering. If \code{NA} (the default
value), the seed is initialised with a random value; this makes sure that
two subsequent calls start with a different seed. Use \code{NULL} to use
the current random seed and also avoid resetting (the behaviour of ggplot
2.2.1 and earlier).}
\item{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 \code{data},}
\item{direction}{One of \code{"as.is"}, code{"alternate"}, \code{"split"},
\code{"split.x"} or \code{"split.y"}. A value of \code{"none"} replicates
the behavior of \code{\link[ggplot2]{position_nudge}}. \code{"split"}
changes the sign of the nudge depending on the direction of the random
jitter applied to each individual observation, which is suitable for
nudging labels outward of the jittered data.}
\item{nudge.from}{One of \code{"original"}, \code{"jittered"},
\code{"original.y"} (or \code{"jittered.x"}), \code{"original.x"} (or
\code{"jittered.y"}). A value of \code{"original"} applies the nudge before
jittering the observations, while \code{"jittered"} applies the nudging
after jittering.}
\item{kept.origin}{One of \code{"original"}, \code{"jittered"} or
\code{"none"}.}
}
\value{
A \code{"Position"} object. The layer function within it returns a
data frame, with the jittered + nudged values in columns \code{x} and
\code{y} and by default the jittered values with no nudging as
\code{x_orig} and \code{y_orig}. With \code{nudge.from = "original"} the
original values with no jitter and no nudge applied are returned as
\code{x_orig} and \code{y_orig}.
}
\description{
\code{position_jitternudge()} combines into one function the action of
\code{\link[ggplot2]{position_jitter}} and
\code{\link[ggplot2]{position_nudge}}. It is useful when labels to jittered
plots and when adding jitter to text labels linked to points plotted without
jitter. 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.
}
\details{
Jitter with \code{position_jitternudge()} is identical to that with
\code{\link[ggplot2]{position_jitter}} while nudging is enhanced compared
to \code{\link[ggplot2]{position_nudge}} by taking into use cases specific
to the combination of jitter and nudge.
There are two posible uses for this function. First it can be used to label
jittered points in a plot. In this case, it is mandatory to use the same
arguments to \code{width}, \code{height} and \code{seed} when passing
\code{position_jitter()} to \code{geom_point()} and
\code{position_jitternudge()} to \code{geom_text()} or to
\code{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
\code{position_jitternudge()} as this last function calls the same code
imported from package 'ggplot2'.
The second use is to jitter labels to be connected to points that have not
been jittered. The return of original positions instead of the jittered
ones is achieved by passing \code{origin = "original"} to override the
default \code{origin = "jittered"}.
}
\note{
When \code{direction = "split"} is used together with no jitter, the
split to left and right, or up and down is done at random.
}
\examples{
jitter <- position_jitter(width = 0.2, height = 2, seed = 123)
jitter_nudge <- position_jitternudge(width = 0.2, height = 2,
seed = 123, x = 0.1,
direction = "split",
nudge.from = "jittered")
ggplot(mpg[1:20, ],
aes(cyl, hwy, label = drv)) +
geom_point(position = jitter) +
geom_text_s(position = jitter_nudge)
jitter_nudge <- position_jitternudge(width = 0.2, height = 2,
seed = 123, x = 0.35,
direction = "split",
nudge.from = "original.x")
ggplot(mpg[1:20, ],
aes(cyl, hwy, label = drv)) +
geom_point(position = jitter) +
geom_text_s(position = jitter_nudge)
jitter <- position_jitter(width = 0, height = 2, seed = 123)
jitter_nudge <- position_jitternudge(width = 0, height = 2,
seed = 123, x = 0.4,
direction = "split",
nudge.from = "original.x")
ggplot(mpg[1:20, ],
aes(cyl, hwy, label = drv)) +
geom_point(position = jitter) +
geom_text_s(position = jitter_nudge)
jitter_nudge <- position_jitternudge(width = 0, height = 2,
seed = 123, x = 0.4,
direction = "alternate",
nudge.from = "original.x")
ggplot(mpg[1:20, ],
aes(cyl, hwy, label = drv)) +
geom_point(position = jitter) +
geom_text_s(position = jitter_nudge)
# No nudge, show how points have moved with jitter
ggplot(mpg[1:20, ],
aes(cyl, hwy, label = drv)) +
geom_point() +
geom_point_s(position =
position_jitter_keep(width = 0.3, height = 2, seed = 123),
color = "red",
arrow = grid::arrow(length = unit(0.4, "lines")))
}
\seealso{
\code{\link[ggplot2]{position_jitter}},
\code{\link[ggplot2]{position_nudge}},
\code{\link[ggrepel]{position_nudge_repel}}.
Other position adjustments:
\code{\link{position_dodgenudge}()},
\code{\link{position_nudge_center}()},
\code{\link{position_nudge_line}()},
\code{\link{position_nudge_to}()},
\code{\link{position_stacknudge}()}
}
\author{
Michał Krassowski, edited by Pedro J. Aphalo.
}
\concept{position adjustments}