From e10663fdbb4faea1f9db1e690a9f6c0526df9b7b Mon Sep 17 00:00:00 2001 From: Carson Date: Fri, 10 Nov 2023 10:17:28 -0600 Subject: [PATCH 1/6] More workarounds for dev ggplot2 --- R/ggplotly.R | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/R/ggplotly.R b/R/ggplotly.R index e6805169a..cdf99f6cc 100644 --- a/R/ggplotly.R +++ b/R/ggplotly.R @@ -398,6 +398,9 @@ gg2list <- function(p, width = NULL, height = NULL, layout$setup_panel_params() data <- layout$map_position(data) + # Hand off position guides to layout + layout$setup_panel_guides(plot$guides, plot$layers) + # Train and map non-position scales npscales <- scales$non_position_scales() if (npscales$n() > 0) { @@ -416,6 +419,15 @@ gg2list <- function(p, width = NULL, height = NULL, data <- lapply(data, scales_map_df, scales = npscales) } + if (npscales$n() > 0) { + plot$guides <- plot$guides$build( + npscales, plot$layers, plot$labels, data + ) + } else { + # Assign empty guides if there are no non-position scales + plot$guides <- ggfun("guides_list")() + } + # Fill in defaults etc. data <- by_layer(function(l, d) l$compute_geom_2(d)) @@ -1005,7 +1017,7 @@ gg2list <- function(p, width = NULL, height = NULL, theme$legend.box.just <- theme$legend.box.just %||% c("center", "center") # scales -> data for guides gdefs <- if (inherits(plot$guides, "ggproto")) { - get_gdefs_ggproto(npscales$scales, theme, plot, layers) + get_gdefs_ggproto(npscales$scales, theme, plot, layers, data) } else { get_gdefs(scales, theme, plot, layers) } @@ -1511,7 +1523,7 @@ scales_add_missing <- function(plot, aesthetics) { # which away from guides_train(), guides_merge(), guides_geom() # towards ggproto methods attached to `plot$guides` # ------------------------------------------------------------------------- -get_gdefs_ggproto <- function(scales, theme, plot, layers) { +get_gdefs_ggproto <- function(scales, theme, plot, layers, layer_data) { # Unfortunate duplication of logic in tidyverse/ggplot2#5428 # which ensures a 1:1 mapping between aesthetics and scales @@ -1520,10 +1532,19 @@ get_gdefs_ggproto <- function(scales, theme, plot, layers) { aesthetics <- unlist(aesthetics, recursive = FALSE, use.names = FALSE) guides <- plot$guides$setup(scales, aesthetics = aesthetics) - guides$train(scales, theme$legend.direction, plot$labels) + if (get_package_version("ggplot2") > "3.4.4") { + guides$train(scales, plot$labels) + } else { + guides$train(scales, theme$legend.direction, plot$labels) + } + if (length(guides$guides) > 0) { guides$merge() - guides$process_layers(layers) + if (get_package_version("ggplot2") > "3.4.4") { + guides$process_layers(layers, layer_data) + } else { + guides$process_layers(layers) + } } # Add old legend/colorbar classes to guide params so that ggplotly() code # can continue to work the same way it always has From 45a309c321c5b420adc10be16660d8acee5b9779 Mon Sep 17 00:00:00 2001 From: Carson Date: Fri, 10 Nov 2023 14:32:53 -0600 Subject: [PATCH 2/6] Simplify --- R/ggplotly.R | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/R/ggplotly.R b/R/ggplotly.R index cdf99f6cc..004702f4c 100644 --- a/R/ggplotly.R +++ b/R/ggplotly.R @@ -419,14 +419,7 @@ gg2list <- function(p, width = NULL, height = NULL, data <- lapply(data, scales_map_df, scales = npscales) } - if (npscales$n() > 0) { - plot$guides <- plot$guides$build( - npscales, plot$layers, plot$labels, data - ) - } else { - # Assign empty guides if there are no non-position scales - plot$guides <- ggfun("guides_list")() - } + plot$guides <- guides_build(plot, npscales, data) # Fill in defaults etc. data <- by_layer(function(l, d) l$compute_geom_2(d)) @@ -1532,20 +1525,14 @@ get_gdefs_ggproto <- function(scales, theme, plot, layers, layer_data) { aesthetics <- unlist(aesthetics, recursive = FALSE, use.names = FALSE) guides <- plot$guides$setup(scales, aesthetics = aesthetics) - if (get_package_version("ggplot2") > "3.4.4") { - guides$train(scales, plot$labels) - } else { - guides$train(scales, theme$legend.direction, plot$labels) - } + guides$train(scales, plot$labels) if (length(guides$guides) > 0) { guides$merge() - if (get_package_version("ggplot2") > "3.4.4") { - guides$process_layers(layers, layer_data) - } else { - guides$process_layers(layers) - } + # TODO: seems as though this should be dropping guides? + guides$process_layers(layers, layer_data) } + # Add old legend/colorbar classes to guide params so that ggplotly() code # can continue to work the same way it always has for (i in which(vapply(guides$guides, inherits, logical(1), "GuideColourbar"))) { @@ -1557,6 +1544,17 @@ get_gdefs_ggproto <- function(scales, theme, plot, layers, layer_data) { guides$params } +guides_build <- function(plot, npscales, data) { + if (!is.function(plot$guides$build)) { + return(plot$guides) + } + if (npscales$n() > 0) { + plot$guides$build(npscales, plot$layers, plot$labels, data) + } else { + ggfun("guides_list")() + } +} + get_gdefs <- function(scales, theme, plot, layers) { gdefs <- ggfun("guides_train")(scales, theme, plot$guides, plot$labels) if (length(gdefs) > 0) { From e496c868f3b21fc207635bfaeed3ccccace3d9b4 Mon Sep 17 00:00:00 2001 From: Carson Date: Fri, 10 Nov 2023 14:38:10 -0600 Subject: [PATCH 3/6] try not explcitly building --- R/ggplotly.R | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/R/ggplotly.R b/R/ggplotly.R index 004702f4c..e6a63b0f1 100644 --- a/R/ggplotly.R +++ b/R/ggplotly.R @@ -419,8 +419,6 @@ gg2list <- function(p, width = NULL, height = NULL, data <- lapply(data, scales_map_df, scales = npscales) } - plot$guides <- guides_build(plot, npscales, data) - # Fill in defaults etc. data <- by_layer(function(l, d) l$compute_geom_2(d)) @@ -1544,17 +1542,6 @@ get_gdefs_ggproto <- function(scales, theme, plot, layers, layer_data) { guides$params } -guides_build <- function(plot, npscales, data) { - if (!is.function(plot$guides$build)) { - return(plot$guides) - } - if (npscales$n() > 0) { - plot$guides$build(npscales, plot$layers, plot$labels, data) - } else { - ggfun("guides_list")() - } -} - get_gdefs <- function(scales, theme, plot, layers) { gdefs <- ggfun("guides_train")(scales, theme, plot$guides, plot$labels) if (length(gdefs) > 0) { From 720caf9858ab6e8b5184382f12cce361d190f210 Mon Sep 17 00:00:00 2001 From: Carson Sievert Date: Fri, 10 Nov 2023 14:55:15 -0600 Subject: [PATCH 4/6] whitespace --- R/ggplotly.R | 3 --- 1 file changed, 3 deletions(-) diff --git a/R/ggplotly.R b/R/ggplotly.R index e6a63b0f1..9f30bb810 100644 --- a/R/ggplotly.R +++ b/R/ggplotly.R @@ -1524,13 +1524,10 @@ get_gdefs_ggproto <- function(scales, theme, plot, layers, layer_data) { guides <- plot$guides$setup(scales, aesthetics = aesthetics) guides$train(scales, plot$labels) - if (length(guides$guides) > 0) { guides$merge() - # TODO: seems as though this should be dropping guides? guides$process_layers(layers, layer_data) } - # Add old legend/colorbar classes to guide params so that ggplotly() code # can continue to work the same way it always has for (i in which(vapply(guides$guides, inherits, logical(1), "GuideColourbar"))) { From 8f0090c1c572319f5b96936336d52f6254b8a9a3 Mon Sep 17 00:00:00 2001 From: Carson Date: Fri, 10 Nov 2023 15:00:52 -0600 Subject: [PATCH 5/6] Update news --- NEWS.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/NEWS.md b/NEWS.md index 11ef7b536..2cd1a650e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,9 @@ # plotly (development version) +## Improvements + +* `ggplotly()` now works better with the development version of ggplot2 (> v3.4.4). (#2315) + # 4.10.3 ## Improvements From 6e81478feed25bde7abb3e68946becf839808b49 Mon Sep 17 00:00:00 2001 From: Carson Date: Fri, 10 Nov 2023 15:08:38 -0600 Subject: [PATCH 6/6] No need for setup_panel_guides() either --- R/ggplotly.R | 3 --- 1 file changed, 3 deletions(-) diff --git a/R/ggplotly.R b/R/ggplotly.R index 9f30bb810..2759ef27b 100644 --- a/R/ggplotly.R +++ b/R/ggplotly.R @@ -398,9 +398,6 @@ gg2list <- function(p, width = NULL, height = NULL, layout$setup_panel_params() data <- layout$map_position(data) - # Hand off position guides to layout - layout$setup_panel_guides(plot$guides, plot$layers) - # Train and map non-position scales npscales <- scales$non_position_scales() if (npscales$n() > 0) {