@@ -68,30 +68,39 @@ PositionJitter <- ggproto("PositionJitter", Position,
68
68
seed <- self $ seed
69
69
}
70
70
list (
71
- width = self $ width % || % (resolution( data $ x , zero = FALSE , TRUE ) * 0.4 ) ,
72
- height = self $ height % || % (resolution( data $ y , zero = FALSE , TRUE ) * 0.4 ) ,
71
+ width = self $ width ,
72
+ height = self $ height ,
73
73
seed = seed
74
74
)
75
75
},
76
76
77
- compute_layer = function (self , data , params , layout ) {
78
- trans_x <- if (params $ width > 0 ) function (x ) jitter(x , amount = params $ width )
79
- trans_y <- if (params $ height > 0 ) function (x ) jitter(x , amount = params $ height )
80
-
81
- # Make sure x and y jitter is only calculated once for all position aesthetics
82
- x_aes <- intersect(ggplot_global $ x_aes , names(data ))
83
- x <- if (length(x_aes ) == 0 ) 0 else data [[x_aes [1 ]]]
84
- y_aes <- intersect(ggplot_global $ y_aes , names(data ))
85
- y <- if (length(y_aes ) == 0 ) 0 else data [[y_aes [1 ]]]
86
- dummy_data <- data_frame0(x = x , y = y , .size = nrow(data ))
87
- fixed_jitter <- with_seed_null(params $ seed , transform_position(dummy_data , trans_x , trans_y ))
88
- x_jit <- fixed_jitter $ x - x
89
- y_jit <- fixed_jitter $ y - y
90
- # Avoid nan values, if x or y has Inf values
91
- x_jit [is.infinite(x )] <- 0
92
- y_jit [is.infinite(y )] <- 0
93
-
94
- # Apply jitter
95
- transform_position(data , function (x ) x + x_jit , function (x ) x + y_jit )
77
+ compute_panel = function (self , data , params , scales ) {
78
+ compute_jitter(data , params $ width , params $ height , seed = params $ seed )
96
79
}
97
80
)
81
+
82
+ compute_jitter <- function (data , width = NULL , height = NULL , seed = NA ) {
83
+
84
+ width <- width %|| % (resolution(data $ x , zero = FALSE , TRUE ) * 0.4 )
85
+ height <- height %|| % (resolution(data $ y , zero = FALSE , TRUE ) * 0.4 )
86
+
87
+ trans_x <- if (width > 0 ) function (x ) jitter(x , amount = width )
88
+ trans_y <- if (height > 0 ) function (x ) jitter(x , amount = height )
89
+
90
+ x_aes <- intersect(ggplot_global $ x_aes , names(data ))
91
+ x <- if (length(x_aes ) == 0 ) 0 else data [[x_aes [1 ]]]
92
+
93
+ y_aes <- intersect(ggplot_global $ y_aes , names(data ))
94
+ y <- if (length(y_aes ) == 0 ) 0 else data [[y_aes [1 ]]]
95
+
96
+ jitter <- data_frame0(x = x , y = y , .size = nrow(data ))
97
+ jitter <- with_seed_null(seed , transform_position(jitter , trans_x , trans_y ))
98
+
99
+ x_jit <- jitter $ x - x
100
+ x_jit [is.infinite(x )] <- 0
101
+
102
+ y_jit <- jitter $ y - y
103
+ y_jit [is.infinite(y )] <- 0
104
+
105
+ transform_position(data , function (x ) x + x_jit , function (x ) x + y_jit )
106
+ }
0 commit comments