-
Notifications
You must be signed in to change notification settings - Fork 10
/
position-nudge-dodge2.R
104 lines (93 loc) · 4.3 KB
/
position-nudge-dodge2.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
#' @rdname position_dodgenudge
#'
#' @export
#'
position_dodge2nudge <-
function(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")) {
preserve <- rlang::arg_match(preserve)
direction <- rlang::arg_match(direction)
kept.origin <- rlang::arg_match(kept.origin)
ggplot2::ggproto(NULL, PositionDodgeAndNudge,
x = x,
y = y,
.fun_x = switch(direction,
none = function(x) {1},
split = sign,
split.y = function(x) {1},
split.x = sign,
center = sign,
function(x) {1}),
.fun_y = switch(direction,
none = function(x) {1},
split = sign,
split.x = function(x) {1},
split.y = sign,
center = sign,
function(x) {1}),
kept.origin = kept.origin,
width = width,
preserve = rlang::arg_match(preserve),
padding = padding,
reverse = reverse
)
}
#' @rdname ggpp-ggproto
#' @format NULL
#' @usage NULL
#' @noRd
PositionDodgeAndNudge <-
ggplot2::ggproto("PositionDodgeAndNudge", ggplot2::PositionDodge2,
x = 0,
y = 0,
setup_params = function(self, data) {
c(
list(nudge_x = self$x, nudge_y = self$y,
.fun_x = self$.fun_x, .fun_y = self$.fun_y,
kept.origin = self$kept.origin),
ggplot2::ggproto_parent(ggplot2::PositionDodge2, self)$setup_params(data)
)
},
compute_layer = function(self, data, params, layout) {
x_orig <- data$x
y_orig <- data$y
# operate on the dodged positions
data = ggplot2::ggproto_parent(ggplot2::PositionDodge2, self)$compute_layer(data, params, layout)
x_dodged <- data$x
y_dodged <- data$y
# transform only the dimensions for which non-zero nudging is requested
if (any(params$nudge_x != 0)) {
if (any(params$nudge_y != 0)) {
data <- ggplot2::transform_position(data,
function(x) x + params$nudge_x * params$.fun_x(x),
function(y) y + params$nudge_y * params$.fun_y(y))
} else {
data <- ggplot2::transform_position(data,
function(x) x + params$nudge_x * params$.fun_x(x),
NULL)
}
} else if (any(params$nudge_y != 0)) {
data <- ggplot2::transform_position(data,
function(x) x,
function(y) y + params$nudge_y * params$.fun_y(y))
}
# add original position
if (params$kept.origin == "dodged") {
data$x_orig <- x_dodged
data$y_orig <- y_dodged
} else if (params$kept.origin == "original") {
data$x_orig <- x_orig
data$y_orig <- y_orig
}
data
},
compute_panel = function(self, data, params, scales) {
ggplot2::ggproto_parent(PositionDodge2, self)$compute_panel(data, params, scales)
}
)