@@ -41,25 +41,9 @@ layer2traces <- function(l, d, misc) {
4141 g $ geom <- " smoothLine"
4242 }
4343 }
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"
6347 bargap <- 0
6448 }
6549
@@ -187,15 +171,14 @@ layer2traces <- function(l, d, misc) {
187171 lapply(df.list , function (df ){
188172 params <- basic $ params
189173 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 ],
191175 params = params )
192176 })
193177 }
194178 }
195-
196179 # Split hline and vline when multiple panels or intercepts:
197180 # Need multiple traces accordingly.
198- if (g $ geom == " hline" || g $ geom == " vline" ) {
181+ if (g $ geom %in% c( " hline" , " vline" ) ) {
199182 intercept <- paste0(ifelse(g $ geom == " hline" , " y" , " x" ), " intercept" )
200183 vec.list <- basic $ data [c(" PANEL" , intercept )]
201184 df.list <- split(basic $ data , vec.list , drop = TRUE )
@@ -221,7 +204,6 @@ layer2traces <- function(l, d, misc) {
221204 }
222205 traces <- NULL
223206 names.in.legend <- NULL
224-
225207 for (data.i in seq_along(data.list )) {
226208 data.params <- data.list [[data.i ]]
227209 data.params $ params $ stat.type <- l $ stat $ objname
@@ -265,18 +247,19 @@ layer2traces <- function(l, d, misc) {
265247 if (is.null(tr $ name ) || tr $ name %in% names.in.legend )
266248 tr $ showlegend <- FALSE
267249 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"
275258 }
259+
276260 traces <- c(traces , list (tr ))
277261 }
278262
279-
280263 sort.val <- sapply(traces , function (tr ){
281264 rank.val <- unlist(tr $ sort )
282265 if (is.null(rank.val )){
@@ -362,25 +345,19 @@ toBasic <- list(
362345 g $ data <- g $ prestats.data
363346 g
364347 },
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 ), ]
373351 g
374352 },
375353 contour = function (g ) {
376354 g $ data <- g $ prestats.data
377355 g
378356 },
379357 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"
384361 g
385362 },
386363 density2d = function (g ) {
@@ -594,40 +571,25 @@ geom2trace <- list(
594571 L
595572 },
596573 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" ,
598585 name = params $ name ,
599586 text = data $ text ,
600587 marker = list (color = toRGB(params $ fill )))
601-
602588 if (! is.null(params $ colour )) {
603589 L $ marker $ line <- list (color = toRGB(params $ colour ))
604590 L $ marker $ line $ width <- if (is.null(params $ size )) 1 else params $ size
605591 }
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
631593 L
632594 },
633595 step = function (data , params ) {
@@ -666,15 +628,6 @@ geom2trace <- list(
666628 L $ contours = list (coloring = " lines" )
667629 L
668630 },
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- },
678631 density2d = function (data , params ) {
679632 L <- list (x = data $ x ,
680633 y = data $ y ,
0 commit comments