21
21
# ' geom_point(pch = 21, position = position_jitterdodge())
22
22
position_jitterdodge <- function (jitter.width = NULL , jitter.height = 0 ,
23
23
dodge.width = 0.75 , reverse = FALSE ,
24
+ preserve = " total" ,
24
25
seed = NA ) {
25
26
if (! is.null(seed ) && is.na(seed )) {
26
27
seed <- sample.int(.Machine $ integer.max , 1L )
@@ -31,6 +32,7 @@ position_jitterdodge <- function(jitter.width = NULL, jitter.height = 0,
31
32
jitter.width = jitter.width ,
32
33
jitter.height = jitter.height ,
33
34
dodge.width = dodge.width ,
35
+ preserve = arg_match0(preserve , c(" total" , " single" )),
34
36
reverse = reverse ,
35
37
seed = seed
36
38
)
@@ -45,6 +47,8 @@ PositionJitterdodge <- ggproto("PositionJitterdodge", Position,
45
47
jitter.height = NULL ,
46
48
dodge.width = NULL ,
47
49
reverse = NULL ,
50
+ default_aes = aes(order = NULL ),
51
+ preserve = " total" ,
48
52
49
53
required_aes = c(" x" , " y" ),
50
54
@@ -53,27 +57,37 @@ PositionJitterdodge <- ggproto("PositionJitterdodge", Position,
53
57
data <- flip_data(data , flipped_aes )
54
58
width <- self $ jitter.width %|| % (resolution(data $ x , zero = FALSE , TRUE ) * 0.4 )
55
59
56
- ndodge <- vec_unique(data [c(" group" , " PANEL" , " x" )])
57
- ndodge <- vec_group_id(ndodge [c(" PANEL" , " x" )])
58
- ndodge <- max(tabulate(ndodge , attr(ndodge , " n" )))
60
+ if (identical(self $ preserve , " total" )) {
61
+ n <- NULL
62
+ } else {
63
+ n <- vec_unique(data [c(" group" , " PANEL" , " x" )])
64
+ n <- vec_group_id(n [c(" PANEL" , " x" )])
65
+ n <- max(tabulate(n , attr(n , " n" )))
66
+ }
59
67
60
68
list (
61
69
dodge.width = self $ dodge.width %|| % 0.75 ,
62
70
jitter.height = self $ jitter.height %|| % 0 ,
63
- jitter.width = width / (ndodge + 2 ),
71
+ jitter.width = width / ((n %|| % 1 ) + 2 ),
72
+ n = n ,
64
73
seed = self $ seed ,
65
74
flipped_aes = flipped_aes ,
66
75
reverse = self $ reverse %|| % FALSE
67
76
)
68
77
},
69
78
79
+ setup_data = function (self , data , params ) {
80
+ PositionDodge $ setup_data(data = data , params = params )
81
+ },
82
+
70
83
compute_panel = function (data , params , scales ) {
71
84
data <- flip_data(data , params $ flipped_aes )
72
85
data <- collide(
73
86
data ,
74
87
params $ dodge.width ,
75
- " position_jitterdodge" ,
88
+ name = " position_jitterdodge" ,
76
89
strategy = pos_dodge ,
90
+ n = params $ n ,
77
91
check.width = FALSE ,
78
92
reverse = ! params $ reverse # for consistency with `position_dodge2()`
79
93
)
0 commit comments