Skip to content

Commit 5b73531

Browse files
committed
Also accomodate for guide changes made in tidyverse/ggplot2#4879
1 parent 8f79eab commit 5b73531

File tree

1 file changed

+40
-7
lines changed

1 file changed

+40
-7
lines changed

R/ggplotly.R

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,12 +1004,12 @@ gg2list <- function(p, width = NULL, height = NULL,
10041004
# justification of legend boxes
10051005
theme$legend.box.just <- theme$legend.box.just %||% c("center", "center")
10061006
# scales -> data for guides
1007-
gdefs <- ggfun("guides_train")(scales, theme, plot$guides, plot$labels)
1008-
if (length(gdefs) > 0) {
1009-
gdefs <- ggfun("guides_merge")(gdefs)
1010-
gdefs <- ggfun("guides_geom")(gdefs, layers, plot$mapping)
1007+
gdefs <- if (inherits(plot$guides, "ggproto")) {
1008+
get_gdefs_ggproto(npscales$scales, theme, plot, layers)
1009+
} else {
1010+
get_gdefs(scales, theme, plot, layers)
10111011
}
1012-
1012+
10131013
# colourbar -> plotly.js colorbar
10141014
colorbar <- compact(lapply(gdefs, gdef2trace, theme, gglayout))
10151015
nguides <- length(colorbar) + gglayout$showlegend
@@ -1461,8 +1461,9 @@ getAesMap <- function(plot, layer) {
14611461
}
14621462

14631463
# ------------------------------------------------------------------
1464-
# Handle compatibility for changes in ggplot2 >v3.4.2 (#5144),
1465-
# which removed these functions in favor of scale/plot methods
1464+
# Handle compatibility for changes in ggplot2 >v3.4.2 (specifically #5144),
1465+
# which moved away from scales_transform_df(), scales_train_df(), etc
1466+
# towards ggproto methods attached to `scales`
14661467
# ------------------------------------------------------------------
14671468
scales_transform_df <- function(scales, df) {
14681469
if (is.function(scales$transform_df)) {
@@ -1495,3 +1496,35 @@ scales_add_missing <- function(plot, aesthetics) {
14951496
ggfun("scales_add_missing")(plot, aesthetics, plot$plot_env)
14961497
}
14971498
}
1499+
1500+
# -------------------------------------------------------------------------
1501+
# Handle compatibility for changes in ggplot2 >v3.4.2 (specifically #4879),
1502+
# which away from guides_train(), guides_merge(), guides_geom()
1503+
# towards ggproto methods attached to `plot$guides`
1504+
# -------------------------------------------------------------------------
1505+
get_gdefs_ggproto <- function(scales, theme, plot, layers) {
1506+
guides <- plot$guides$setup(scales)
1507+
guides$train(scales, theme$legend.direction, plot$labels)
1508+
if (length(guides$guides) > 0) {
1509+
guides$merge()
1510+
guides$process_layers(layers)
1511+
}
1512+
# Add old legend/colorbar classes to guide params so that ggplotly() code
1513+
# can continue to work the same way it always has
1514+
for (i in which(vapply(guides$guides, inherits, logical(1), "GuideColourbar"))) {
1515+
guides$params[[i]] <- prefix_class(guides$params[[i]], "colorbar")
1516+
}
1517+
for (i in which(vapply(guides$guides, inherits, logical(1), "GuideLegend"))) {
1518+
guides$params[[i]] <- prefix_class(guides$params[[i]], "legend")
1519+
}
1520+
guides$params
1521+
}
1522+
1523+
get_gdefs <- function(scales, theme, plot, layers) {
1524+
gdefs <- ggfun("guides_train")(scales, theme, plot$guides, plot$labels)
1525+
if (length(gdefs) > 0) {
1526+
gdefs <- ggfun("guides_merge")(gdefs)
1527+
gdefs <- ggfun("guides_geom")(gdefs, layers, plot$mapping)
1528+
}
1529+
gdefs
1530+
}

0 commit comments

Comments
 (0)