@@ -398,6 +398,9 @@ gg2list <- function(p, width = NULL, height = NULL,
398398 layout $ setup_panel_params()
399399 data <- layout $ map_position(data )
400400
401+ # Hand off position guides to layout
402+ layout $ setup_panel_guides(plot $ guides , plot $ layers )
403+
401404 # Train and map non-position scales
402405 npscales <- scales $ non_position_scales()
403406 if (npscales $ n() > 0 ) {
@@ -416,6 +419,15 @@ gg2list <- function(p, width = NULL, height = NULL,
416419 data <- lapply(data , scales_map_df , scales = npscales )
417420 }
418421
422+ if (npscales $ n() > 0 ) {
423+ plot $ guides <- plot $ guides $ build(
424+ npscales , plot $ layers , plot $ labels , data
425+ )
426+ } else {
427+ # Assign empty guides if there are no non-position scales
428+ plot $ guides <- ggfun(" guides_list" )()
429+ }
430+
419431 # Fill in defaults etc.
420432 data <- by_layer(function (l , d ) l $ compute_geom_2(d ))
421433
@@ -1005,7 +1017,7 @@ gg2list <- function(p, width = NULL, height = NULL,
10051017 theme $ legend.box.just <- theme $ legend.box.just %|| % c(" center" , " center" )
10061018 # scales -> data for guides
10071019 gdefs <- if (inherits(plot $ guides , " ggproto" )) {
1008- get_gdefs_ggproto(npscales $ scales , theme , plot , layers )
1020+ get_gdefs_ggproto(npscales $ scales , theme , plot , layers , data )
10091021 } else {
10101022 get_gdefs(scales , theme , plot , layers )
10111023 }
@@ -1511,7 +1523,7 @@ scales_add_missing <- function(plot, aesthetics) {
15111523# which away from guides_train(), guides_merge(), guides_geom()
15121524# towards ggproto methods attached to `plot$guides`
15131525# -------------------------------------------------------------------------
1514- get_gdefs_ggproto <- function (scales , theme , plot , layers ) {
1526+ get_gdefs_ggproto <- function (scales , theme , plot , layers , layer_data ) {
15151527
15161528 # Unfortunate duplication of logic in tidyverse/ggplot2#5428
15171529 # which ensures a 1:1 mapping between aesthetics and scales
@@ -1520,10 +1532,19 @@ get_gdefs_ggproto <- function(scales, theme, plot, layers) {
15201532 aesthetics <- unlist(aesthetics , recursive = FALSE , use.names = FALSE )
15211533
15221534 guides <- plot $ guides $ setup(scales , aesthetics = aesthetics )
1523- guides $ train(scales , theme $ legend.direction , plot $ labels )
1535+ if (get_package_version(" ggplot2" ) > " 3.4.4" ) {
1536+ guides $ train(scales , plot $ labels )
1537+ } else {
1538+ guides $ train(scales , theme $ legend.direction , plot $ labels )
1539+ }
1540+
15241541 if (length(guides $ guides ) > 0 ) {
15251542 guides $ merge()
1526- guides $ process_layers(layers )
1543+ if (get_package_version(" ggplot2" ) > " 3.4.4" ) {
1544+ guides $ process_layers(layers , layer_data )
1545+ } else {
1546+ guides $ process_layers(layers )
1547+ }
15271548 }
15281549 # Add old legend/colorbar classes to guide params so that ggplotly() code
15291550 # can continue to work the same way it always has
@@ -1536,6 +1557,10 @@ get_gdefs_ggproto <- function(scales, theme, plot, layers) {
15361557 guides $ params
15371558}
15381559
1560+ guides_train <- function () {
1561+
1562+ }
1563+
15391564get_gdefs <- function (scales , theme , plot , layers ) {
15401565 gdefs <- ggfun(" guides_train" )(scales , theme , plot $ guides , plot $ labels )
15411566 if (length(gdefs ) > 0 ) {
0 commit comments