Skip to content

Commit fe130c6

Browse files
committed
This conversion for geom_bar() works but is not complete
1 parent 580b55c commit fe130c6

File tree

2 files changed

+34
-24
lines changed

2 files changed

+34
-24
lines changed

R/ggplotly.R

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,16 @@ geom2trace <-
165165
L$marker$size <- data$size
166166
}
167167
L
168-
})
168+
},
169+
bar=function(data, params) {
170+
list(x=data$x,
171+
y=data$y,
172+
name=params$name,
173+
text=data$text,
174+
type="bar")
175+
}
176+
)
177+
169178

170179
#' Convert ggplot2 aes to line parameters.
171180
aes2line <- c(linetype="dash",
@@ -175,7 +184,8 @@ aes2line <- c(linetype="dash",
175184
markLegends <-
176185
list(point=c("colour", "fill", "shape"),
177186
path=c("linetype", "size", "colour"),
178-
polygon=c("colour", "fill", "linetype", "size", "group"))
187+
polygon=c("colour", "fill", "linetype", "size", "group"),
188+
bar=c("fill"))
179189

180190
markUnique <- as.character(unique(unlist(markLegends)))
181191

@@ -215,11 +225,11 @@ gg2list <- function(p){
215225
for(i in seq_along(built$plot$layers)){
216226
## This is the layer from the original ggplot object.
217227
L <- p$layers[[i]]
218-
228+
219229
## for each layer, there is a correpsonding data.frame which
220230
## evaluates the aesthetic mapping.
221231
df <- built$data[[i]]
222-
232+
223233
## Test fill and color to see if they encode a quantitative
224234
## variable. In that case, we do not make traces for separate
225235
## colors, since there are too many!
@@ -237,7 +247,7 @@ gg2list <- function(p){
237247
FALSE
238248
})
239249
}
240-
250+
241251
## scales are needed for legend ordering.
242252
for(sc in p$scales$scales){
243253
a <- sc$aesthetics
@@ -248,10 +258,10 @@ gg2list <- function(p){
248258
misc$breaks[[sc$aesthetics]] <- ranks
249259
}
250260
}
251-
261+
252262
## This extracts essential info for this geom/layer.
253263
traces <- layer2traces(L, df, misc)
254-
264+
255265
## Do we really need to coord_transform?
256266
##g$data <- ggplot2:::coord_transform(built$plot$coord, g$data,
257267
## built$panel$ranges[[1]])
@@ -263,7 +273,7 @@ gg2list <- function(p){
263273
# grid 0-1 scale). This allows transformations to be used
264274
# out of the box, with no additional d3 coding.
265275
theme.pars <- ggplot2:::plot_theme(p)
266-
276+
267277
## Flip labels if coords are flipped - transform does not take care
268278
## of this. Do this BEFORE checking if it is blank or not, so that
269279
## individual axes can be hidden appropriately, e.g. #1.
@@ -329,21 +339,21 @@ gg2list <- function(p){
329339
!is.blank(s("axis.ticks.%s"))
330340
layout[[s("%saxis")]] <- ax.list
331341
}
332-
342+
333343
## Remove legend if theme has no legend position
334344
if(theme.pars$legend.position=="none") layout$showlegend <- FALSE
335-
345+
336346
## Main plot title.
337347
layout$title <- built$plot$labels$title
338-
348+
339349
## Background color.
340350
layout$plot_bgcolor <- toRGB(theme.pars$panel.background$fill)
341351
layout$paper_bgcolor <- toRGB(theme.pars$plot.background$fill)
342-
352+
343353
## Legend.
344354
layout$margin$r <- 10
345355
layout$legend <- list(bordercolor="transparent", x=100, y=1/2)
346-
356+
347357
trace.list$kwargs <- list(layout=layout)
348358
trace.list
349359
}
@@ -359,7 +369,7 @@ layer2traces <- function(l, d, misc){
359369
data=d)
360370
## needed for when group, etc. is an expression.
361371
g$aes <- sapply(l$mapping, function(k) as.character(as.expression(k)))
362-
372+
363373
## use un-named parameters so that they will not be exported
364374
## to JSON as a named object, since that causes problems with
365375
## e.g. colour.
@@ -373,41 +383,41 @@ layer2traces <- function(l, d, misc){
373383
## {"bar":"foo"}
374384
names(g$params[[p.name]]) <- NULL
375385
}
376-
386+
377387
## Convert complex ggplot2 geoms so that they are treated as special
378388
## cases of basic geoms. In ggplot2, this processing is done in the
379389
## draw method of the geoms.
380-
390+
381391
## Every plotly trace has one of these types
382392
## type=scatter,bar,box,histogramx,histogram2d,heatmap
383-
393+
384394
## for type=scatter, you can define
385395
## mode=none,markers,lines,lines+markers where "lines" is the
386396
## default for 20 or more points, "lines+markers" is the default for
387397
## <20 points. "none" is useful mainly if fill is used to make area
388398
## plots with no lines.
389-
399+
390400
## marker=list(size,line,color="rgb(54,144,192)",opacity,symbol)
391-
401+
392402
## symbol=circle,square,diamond,cross,x,
393403
## triangle-up,triangle-down,triangle-left,triangle-right
394-
404+
395405
## First convert to a "basic" geom, e.g. segments become lines.
396406
convert <- toBasic[[g$geom]]
397407
basic <- if(is.null(convert)){
398408
g
399409
}else{
400410
convert(g)
401411
}
402-
412+
403413
## Then split on visual characteristics that will get different
404414
## legend entries.
405415
data.list <- if(basic$geom %in% names(markLegends)){
406416
mark.names <- markLegends[[basic$geom]]
407417
## However, continuously colored points are an exception: they do
408418
## not need a legend entry, and they can be efficiently rendered
409419
## using just 1 trace.
410-
420+
411421
## Maybe it is nice to show a legend for continuous points?
412422
## if(basic$geom == "point"){
413423
## to.erase <- names(misc$is.continuous)[misc$is.continuous]
@@ -436,7 +446,7 @@ layer2traces <- function(l, d, misc){
436446
data.list <- structure(list(list(data=basic$data, params=basic$params)),
437447
names=basic$params$name)
438448
}
439-
449+
440450
getTrace <- geom2trace[[basic$geom]]
441451
if(is.null(getTrace)){
442452
stop("conversion not implemented for geom_",

inst/tests/test-ggplot-bar.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ researchers <-
99
gg <- ggplot(researchers, aes(country, papers, fill=field))
1010

1111
test_that("position_dodge is translated to barmode=group", {
12-
gg.dodge <- gg+geom_bar(stat="identity", position="dodge")
12+
gg.dodge <- gg + geom_bar(stat="identity", position="dodge")
1313
L <- gg2list(gg.dodge)
1414
expect_equal(length(L), 3)
1515
trace.names <- sapply(L[1:2], "[[", "name")

0 commit comments

Comments
 (0)