@@ -41,25 +41,9 @@ layer2traces <- function(l, d, misc) {
41
41
g $ geom <- " smoothLine"
42
42
}
43
43
}
44
- # Barmode and bargap
45
- barmode <- " group"
46
- if (g $ geom == " bar" || g $ geom == " histogram" ) {
47
- if (l $ stat $ objname == " bin" ) {
48
- if (g $ geom != " histogram" ) {
49
- warning(" You may want to use geom_histogram." )
50
- }
51
- } else {
52
- bargap <- " default"
53
- }
54
- g $ geom <- " bar" # histogram is just an alias for geom_bar + stat_bin
55
- pos <- l $ position $ .super $ objname
56
- if (pos == " identity" ) {
57
- barmode <- " overlay"
58
- } else if (pos == " stack" ) {
59
- barmode <- " stack"
60
- }
61
- }
62
- if (g $ geom == " density" ) {
44
+ # histogram is essentially a bar chart with no gaps (after stats are computed)
45
+ if (g $ geom == " histogram" ) {
46
+ g $ geom <- " bar"
63
47
bargap <- 0
64
48
}
65
49
@@ -187,15 +171,14 @@ layer2traces <- function(l, d, misc) {
187
171
lapply(df.list , function (df ){
188
172
params <- basic $ params
189
173
params [invariable.names ] <- if (ncol(x <- df [1 , invariable.names ]) > 0 ) x else NULL
190
- list (data = df [other.names ],
174
+ list (data = df [other.names ],
191
175
params = params )
192
176
})
193
177
}
194
178
}
195
-
196
179
# Split hline and vline when multiple panels or intercepts:
197
180
# Need multiple traces accordingly.
198
- if (g $ geom == " hline" || g $ geom == " vline" ) {
181
+ if (g $ geom %in% c( " hline" , " vline" ) ) {
199
182
intercept <- paste0(ifelse(g $ geom == " hline" , " y" , " x" ), " intercept" )
200
183
vec.list <- basic $ data [c(" PANEL" , intercept )]
201
184
df.list <- split(basic $ data , vec.list , drop = TRUE )
@@ -221,7 +204,6 @@ layer2traces <- function(l, d, misc) {
221
204
}
222
205
traces <- NULL
223
206
names.in.legend <- NULL
224
-
225
207
for (data.i in seq_along(data.list )) {
226
208
data.params <- data.list [[data.i ]]
227
209
data.params $ params $ stat.type <- l $ stat $ objname
@@ -265,18 +247,19 @@ layer2traces <- function(l, d, misc) {
265
247
if (is.null(tr $ name ) || tr $ name %in% names.in.legend )
266
248
tr $ showlegend <- FALSE
267
249
names.in.legend <- c(names.in.legend , tr $ name )
268
-
269
- if (g $ geom == " bar" )
270
- tr $ barmode <- barmode
271
-
272
- # Bar Gap
273
- if (exists(" bargap" )) {
274
- tr $ bargap <- bargap
250
+
251
+ # special handling for bars
252
+ if (g $ geom == " bar" ) {
253
+ tr $ bargap <- if (exists(" bargap" )) bargap else " default"
254
+ pos <- l $ position $ .super $ objname
255
+ tr $ barmode <- if (pos %in% c(" identity" , " stack" , " fill" )) {
256
+ " stack"
257
+ } else " group"
275
258
}
259
+
276
260
traces <- c(traces , list (tr ))
277
261
}
278
262
279
-
280
263
sort.val <- sapply(traces , function (tr ){
281
264
rank.val <- unlist(tr $ sort )
282
265
if (is.null(rank.val )){
@@ -362,25 +345,19 @@ toBasic <- list(
362
345
g $ data <- g $ prestats.data
363
346
g
364
347
},
365
- bar = function (g ) {
366
- for (a in c(" fill" , " colour" )){
367
- g $ prestats.data [[a ]] <-
368
- g $ data [[a ]][match(g $ prestats.data $ group , g $ data $ group )]
369
- }
370
- g $ params $ xstart <- min(g $ data $ xmin )
371
- g $ params $ xend <- max(g $ data $ xmax )
372
- g $ data <- g $ prestats.data
348
+ bar = function (g ){
349
+ g <- group2NA(g , " bar" )
350
+ g $ data <- g $ data [! is.na(g $ data $ y ), ]
373
351
g
374
352
},
375
353
contour = function (g ) {
376
354
g $ data <- g $ prestats.data
377
355
g
378
356
},
379
357
density = function (g ) {
380
- g $ params $ xstart <- min(g $ data $ x )
381
- g $ params $ xend <- max(g $ data $ x )
382
- g $ params $ binwidth <- (max(g $ data $ x ) - min(g $ data $ x ))/ 30
383
- g $ data <- g $ prestats.data
358
+ g $ geom <- " area"
359
+ if (is.null(g $ data $ fill ) && is.null(g $ params $ alpha )) g $ params $ alpha <- 0
360
+ if (is.null(g $ data $ colour )) g $ params $ colour <- " black"
384
361
g
385
362
},
386
363
density2d = function (g ) {
@@ -594,40 +571,25 @@ geom2trace <- list(
594
571
L
595
572
},
596
573
bar = function (data , params ) {
597
- L <- list (x = data $ x ,
574
+ x <- if (" x.name" %in% names(data )) data $ x.name else data $ x
575
+ if (inherits(x , " POSIXt" )) {
576
+ # Convert seconds into milliseconds
577
+ x <- as.numeric(x ) * 1000
578
+ } else if (inherits(x , " Date" )) {
579
+ # Convert days into milliseconds
580
+ x <- as.numeric(x ) * 24 * 60 * 60 * 1000
581
+ }
582
+ L <- list (x = x ,
583
+ y = data $ y ,
584
+ type = " bar" ,
598
585
name = params $ name ,
599
586
text = data $ text ,
600
587
marker = list (color = toRGB(params $ fill )))
601
-
602
588
if (! is.null(params $ colour )) {
603
589
L $ marker $ line <- list (color = toRGB(params $ colour ))
604
590
L $ marker $ line $ width <- if (is.null(params $ size )) 1 else params $ size
605
591
}
606
-
607
- if (! is.null(params $ alpha ))
608
- L $ opacity <- params $ alpha
609
-
610
- if (params $ stat.type == " bin" ) {
611
- L $ type <- " histogram"
612
- if (is.null(params $ binwidth )) {
613
- L $ autobinx <- TRUE
614
- } else {
615
- L $ autobinx <- FALSE
616
- L $ xbins = list (start = params $ xstart ,
617
- end = params $ xend ,
618
- size = params $ binwidth )
619
- if (inherits(data $ x.name , " POSIXt" )) {
620
- # Convert seconds into milliseconds
621
- L $ xbins <- lapply(L $ xbins , function (x ) x * 1000 )
622
- } else if (inherits(data $ x.name , " Date" )) {
623
- # Convert days into milliseconds
624
- L $ xbins <- lapply(L $ xbins , function (x ) x * 24 * 60 * 60 * 1000 )
625
- }
626
- }
627
- } else {
628
- L $ y <- data $ y
629
- L $ type <- " bar"
630
- }
592
+ if (! is.null(params $ alpha )) L $ opacity <- params $ alpha
631
593
L
632
594
},
633
595
step = function (data , params ) {
@@ -666,15 +628,6 @@ geom2trace <- list(
666
628
L $ contours = list (coloring = " lines" )
667
629
L
668
630
},
669
- density = function (data , params ) {
670
- L <- list (x = data $ x ,
671
- name = params $ name ,
672
- text = data $ text ,
673
- marker = list (color = toRGB(params $ fill )),
674
- type = " histogram" ,
675
- autobinx = TRUE ,
676
- histnorm = " probability density" )
677
- },
678
631
density2d = function (data , params ) {
679
632
L <- list (x = data $ x ,
680
633
y = data $ y ,
0 commit comments