-
Notifications
You must be signed in to change notification settings - Fork 10
/
position-nudge-to.R
172 lines (164 loc) · 5.91 KB
/
position-nudge-to.R
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
#' Nudge labels to new positions
#'
#' \code{position_nudge_to()} is generally useful for adjusting the position of
#' labels or text, both on a discrete or continuous scale.
#' \code{position_nudge_to()} differs from \code{\link[ggplot2]{position_nudge}}
#' in that the coordinates of the new position are given directly, rather than
#' as a displacement from the original location. It optionally sets an even
#' distance among positions. As other position functions in this package, it
#' preserves the original position to allow the text to be linked back to its
#' original position with a segment or arrow.
#'
#' @family position adjustments
#'
#' @param x,y Coordinates of the destination position. A vector of mode
#' \code{numeric}, that is extended if needed, to the same length as rows
#' there are in \code{data}. The values are applied in the order of the
#' observations in data. The default, \code{NULL}, leaves the original
#' coordinates unchanged.
#' @param x.action,y.action character string, one of \code{"none"}, or
#' \code{"spread"}. With \code{"spread"} evenly distributing the positions
#' within the range of argument \code{x} or \code{y}, if non-null, or the
#' range the variable mapped to \emph{x} or \code{y}, otherwise.
#' @param kept.origin One of \code{"original"} or \code{"none"}.
#'
#' @details The nudged \code{x} or \code{y} replace the original ones in
#' \code{data}, while the original coordinates are returned in \code{x_orig}
#' and \code{y_orig}. Values supported are those of \emph{mode} numeric,
#' thus including dates and times.
#'
#' @note Irrespective of the action, the ordering of rows in \code{data} is
#' preserved.
#'
#' @return A \code{"Position"} object.
#'
#' @seealso \code{\link[ggplot2]{position_nudge}},
#' \code{\link[ggrepel]{position_nudge_repel}}.
#'
#' @export
#'
#' @examples
#' df <- data.frame(
#' x = c(1,3,2,5,4,2.5),
#' y = c(2, 1, 2.5, 1.8, 2.8, 1.5),
#' label = c("abc","cd","d","c","bcd","a")
#' )
#'
#' ggplot(df, aes(x, y, label = label)) +
#' geom_point() +
#' geom_text(position = position_nudge_to(y = 3))
#'
#' ggplot(df, aes(x, y, label = label)) +
#' geom_point() +
#' geom_text_s(position = position_nudge_to(y = 3))
#'
#' ggplot(df, aes(x, y, label = label)) +
#' geom_point() +
#' geom_text_s(position =
#' position_nudge_to(y = 3, x.action = "spread"))
#'
#' ggplot(df, aes(x, y, label = label)) +
#' geom_point() +
#' geom_text_s(position =
#' position_nudge_to(y = 3, x = c(2,4), x.action = "spread"),
#' hjust = "center")
#'
position_nudge_to <-
function(x = NULL,
y = NULL,
x.action = c("none", "spread"),
y.action = c("none", "spread"),
kept.origin = c("original", "none")) {
kept.origin <- rlang::arg_match(kept.origin)
x.action <- rlang::arg_match(x.action)
y.action <- rlang::arg_match(y.action)
stopifnot("'x' must be NULL or of mode numeric" = is.null(x) || mode(x) == "numeric")
stopifnot("'y' must be NULL or of mode numeric" = is.null(y) || mode(y) == "numeric")
ggplot2::ggproto(NULL, PositionNudgeTo,
x = x,
y = y,
x.action = x.action,
y.action = y.action,
kept.origin = kept.origin
)
}
#' @rdname ggpp-ggproto
#' @format NULL
#' @usage NULL
#' @export
PositionNudgeTo <-
ggplot2::ggproto(
"PositionNudgeTo",
Position,
x = NULL,
y = NULL,
setup_params = function(self, data) {
list(x = self$x,
y = self$y,
x.action = self$x.action,
y.action = self$y.action,
kept.origin = self$kept.origin
)
},
compute_layer = function(self, data, params, layout) {
x_orig <- data$x
y_orig <- data$y
# compute/convert x nudges
if (is.null(params$x)) {
if (params$x.action == "none") {
params$x <- rep_len(0, nrow(data))
} else if (params$x.action == "spread") {
params$x <- range(x_orig)
}
} else if (is.numeric(params$x)) {
if (params$x.action == "none") {
params$x <- rep_len(params$x, nrow(data)) - x_orig
} else if (params$x.action == "spread") {
params$x <- range(params$x)
}
}
if (params$x.action == "spread") {
# evenly spaced sequence ordered as in data
params$x <- seq(from = params$x[1],
to = params$x[2],
length.out = nrow(data))[order(order(data$x))] - x_orig
}
# compute/convert y nudges
if (is.null(params$y)) {
if (params$y.action == "none") {
params$y <- rep_len(0, nrow(data))
} else if (params$y.action == "spread") {
params$y <- range(y_orig)
}
} else if (is.numeric(params$y)) {
if (params$y.action == "none") {
params$y <- rep_len(params$y, nrow(data)) - y_orig
} else if (params$y.action == "spread") {
params$y <- range(params$y)
}
}
if (params$y.action == "spread") {
# evenly spaced sequence ordered as in data
params$y <- seq(from = params$y[1],
to = params$y[2],
length.out = nrow(data))[order(order(data$y))] - y_orig
}
# As in 'ggplot2' we apply the nudge to xmin, xmax, xend, ymin, ymax, and yend.
# Transform the dimensions for which not all nudges are zero
if (any(params$x != 0)) {
if (any(params$y != 0)) {
data <- transform_position(data, function(x) x + params$x, function(y) y + params$y)
} else {
data <- transform_position(data, function(x) x + params$x, NULL)
}
} else if (any(params$y != 0)) {
data <- transform_position(data, NULL, function(y) y + params$y)
}
# add original position
if (params$kept.origin == "original") {
data$x_orig <- x_orig
data$y_orig <- y_orig
}
data
}
)