Skip to content

Commit cbe301b

Browse files
committed
Avoid geom specific code in ggplotly.R
1 parent 4ac88f0 commit cbe301b

File tree

2 files changed

+48
-23
lines changed

2 files changed

+48
-23
lines changed

R/ggplotly.R

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -217,24 +217,8 @@ gg2list <- function(p){
217217
}
218218

219219
# This extracts essential info for this geom/layer.
220-
if (L$geom$objname == "smooth") {
221-
# smooth is really a line + ribbon geom
222-
# draw ribbon (unless se = FALSE)
223-
misc$smoothRibbon <- TRUE
224-
trace1 <- if (isTRUE(L$stat_params$se == FALSE)) {
225-
NULL
226-
} else {
227-
layer2traces(L, df, misc)
228-
}
229-
misc$smoothRibbon <- FALSE
230-
# always draw the line
231-
misc$smoothLine <- TRUE
232-
trace2 <- layer2traces(L, df, misc)
233-
trace2 <- lapply(trace2, function(x) { x$showlegend <- FALSE; x })
234-
traces <- c(trace1, trace2)
235-
} else {
236-
traces <- layer2traces(L, df, misc)
237-
}
220+
traces <- layer2traces(L, df, misc)
221+
238222
possible.legends <- markLegends[[L$geom$objname]]
239223
actual.legends <- possible.legends[possible.legends %in% names(L$mapping)]
240224
layer.legends[[paste(i)]] <- actual.legends

R/trace_generation.R

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@ layer2traces <- function(l, d, misc) {
1313
g <- list(geom=l$geom$objname,
1414
data=not.na(d),
1515
prestats.data=not.na(misc$prestats.data))
16-
if (g$geom == "smooth") {
17-
if (isTRUE(misc$smoothRibbon)) g$geom <- "smoothRibbon"
18-
if (isTRUE(misc$smoothLine)) g$geom <- "smoothLine"
19-
}
16+
2017
# needed for when group, etc. is an expression.
2118
g$aes <- sapply(l$mapping, function(k) as.character(as.expression(k)))
2219
# Partial conversion for geom_violin (Plotly does not offer KDE yet)
@@ -26,6 +23,22 @@ layer2traces <- function(l, d, misc) {
2623
probability density estimation is not supported in Plotly yet.")
2724
}
2825

26+
# geom_smooth() means geom_line() + geom_ribbon()
27+
# Note the line is always drawn, but ribbon is not if se = FALSE.
28+
if (g$geom == "smooth") {
29+
# If smoothLine has been compiled already, consider smoothRibbon.
30+
if (isTRUE(misc$smoothLine)) {
31+
misc$smoothLine <- FALSE
32+
if (isTRUE(L$stat_param$se == FALSE)) {
33+
return(NULL)
34+
} else {
35+
g$geom <- "smoothRibbon"
36+
}
37+
} else {
38+
misc$smoothLine <- TRUE
39+
g$geom <- "smoothLine"
40+
}
41+
}
2942
# Barmode and bargap
3043
barmode <- "group"
3144
if (g$geom == "bar" || g$geom == "histogram") {
@@ -285,7 +298,13 @@ layer2traces <- function(l, d, misc) {
285298
}
286299
no.sort[[tr.i]]$sort <- NULL
287300
}
288-
no.sort
301+
# if line portion of geom_smooth was compiled, call layer2traces()
302+
# again for ribbon portion
303+
if (isTRUE(misc$smoothLine)) {
304+
c(layer2traces(l, d, misc), no.sort)
305+
} else {
306+
no.sort
307+
}
289308
}#layer2traces
290309

291310

@@ -652,4 +671,26 @@ geom2trace <- list(
652671
mode="lines",
653672
line=paramORdefault(params, aes2line, line.defaults))
654673
}
674+
# smooth=function(data, params) {
675+
# if (isTRUE(params$se == FALSE)) {
676+
# L1 <- NULL
677+
# } else {
678+
# L1 <- list(x=c(data$x[1], data$x, rev(data$x)),
679+
# y=c(data$ymin[1], data$ymax, rev(data$ymin)),
680+
# type="scatter",
681+
# line=paramORdefault(params, aes2line, ribbon.line.defaults),
682+
# fill="tonexty",
683+
# fillcolor=toFill(params$fill, ifelse(is.null(params$alpha), 1,
684+
# params$alpha)))
685+
# }
686+
# # $showlegend <- FALSE
687+
# c(L1,
688+
# list(x=data$x,
689+
# y=data$y,
690+
# name=params$name,
691+
# text=data$text,
692+
# type="scatter",
693+
# mode="lines",
694+
# line=paramORdefault(params, aes2line, line.defaults)))
695+
# }
655696
)

0 commit comments

Comments
 (0)