@@ -398,6 +398,9 @@ gg2list <- function(p, width = NULL, height = NULL,
398
398
layout $ setup_panel_params()
399
399
data <- layout $ map_position(data )
400
400
401
+ # Hand off position guides to layout
402
+ layout $ setup_panel_guides(plot $ guides , plot $ layers )
403
+
401
404
# Train and map non-position scales
402
405
npscales <- scales $ non_position_scales()
403
406
if (npscales $ n() > 0 ) {
@@ -416,6 +419,15 @@ gg2list <- function(p, width = NULL, height = NULL,
416
419
data <- lapply(data , scales_map_df , scales = npscales )
417
420
}
418
421
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
+
419
431
# Fill in defaults etc.
420
432
data <- by_layer(function (l , d ) l $ compute_geom_2(d ))
421
433
@@ -1005,7 +1017,7 @@ gg2list <- function(p, width = NULL, height = NULL,
1005
1017
theme $ legend.box.just <- theme $ legend.box.just %|| % c(" center" , " center" )
1006
1018
# scales -> data for guides
1007
1019
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 )
1009
1021
} else {
1010
1022
get_gdefs(scales , theme , plot , layers )
1011
1023
}
@@ -1511,7 +1523,7 @@ scales_add_missing <- function(plot, aesthetics) {
1511
1523
# which away from guides_train(), guides_merge(), guides_geom()
1512
1524
# towards ggproto methods attached to `plot$guides`
1513
1525
# -------------------------------------------------------------------------
1514
- get_gdefs_ggproto <- function (scales , theme , plot , layers ) {
1526
+ get_gdefs_ggproto <- function (scales , theme , plot , layers , layer_data ) {
1515
1527
1516
1528
# Unfortunate duplication of logic in tidyverse/ggplot2#5428
1517
1529
# which ensures a 1:1 mapping between aesthetics and scales
@@ -1520,10 +1532,19 @@ get_gdefs_ggproto <- function(scales, theme, plot, layers) {
1520
1532
aesthetics <- unlist(aesthetics , recursive = FALSE , use.names = FALSE )
1521
1533
1522
1534
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
+
1524
1541
if (length(guides $ guides ) > 0 ) {
1525
1542
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
+ }
1527
1548
}
1528
1549
# Add old legend/colorbar classes to guide params so that ggplotly() code
1529
1550
# can continue to work the same way it always has
0 commit comments