@@ -314,7 +314,7 @@ gg2list <- function(p, width = NULL, height = NULL,
314
314
})
315
315
316
316
# Transform all scales
317
- data <- lapply(data , ggfun( " scales_transform_df" ) , scales = scales )
317
+ data <- lapply(data , scales_transform_df , scales = scales )
318
318
319
319
# Map and train positions so that statistics have access to ranges
320
320
# and all positions are numeric
@@ -368,7 +368,7 @@ gg2list <- function(p, width = NULL, height = NULL,
368
368
data <- by_layer(function (l , d ) l $ map_statistic(d , plot ))
369
369
370
370
# Make sure missing (but required) aesthetics are added
371
- ggfun( " scales_add_missing" ) (plot , c(" x" , " y" ), plot $ plot_env )
371
+ scales_add_missing(plot , c(" x" , " y" ))
372
372
373
373
# Reparameterise geoms from (e.g.) y and width to ymin and ymax
374
374
data <- by_layer(function (l , d ) l $ compute_geom_1(d ))
@@ -401,7 +401,7 @@ gg2list <- function(p, width = NULL, height = NULL,
401
401
# Train and map non-position scales
402
402
npscales <- scales $ non_position_scales()
403
403
if (npscales $ n() > 0 ) {
404
- lapply(data , ggfun( " scales_train_df" ) , scales = npscales )
404
+ lapply(data , scales_train_df , scales = npscales )
405
405
# this for loop is unique to plotly -- it saves the "domain"
406
406
# of each non-positional scale for display in tooltips
407
407
for (sc in npscales $ scales ) {
@@ -413,7 +413,7 @@ gg2list <- function(p, width = NULL, height = NULL,
413
413
d
414
414
})
415
415
}
416
- data <- lapply(data , ggfun( " scales_map_df" ) , scales = npscales )
416
+ data <- lapply(data , scales_map_df , scales = npscales )
417
417
}
418
418
419
419
# Fill in defaults etc.
@@ -1004,12 +1004,12 @@ gg2list <- function(p, width = NULL, height = NULL,
1004
1004
# justification of legend boxes
1005
1005
theme $ legend.box.just <- theme $ legend.box.just %|| % c(" center" , " center" )
1006
1006
# 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 )
1011
1011
}
1012
-
1012
+
1013
1013
# colourbar -> plotly.js colorbar
1014
1014
colorbar <- compact(lapply(gdefs , gdef2trace , theme , gglayout ))
1015
1015
nguides <- length(colorbar ) + gglayout $ showlegend
@@ -1403,12 +1403,21 @@ gdef2trace <- function(gdef, theme, gglayout) {
1403
1403
if (inherits(gdef , " colorbar" )) {
1404
1404
# sometimes the key has missing values, which we can ignore
1405
1405
gdef $ key <- gdef $ key [! is.na(gdef $ key $ .value ), ]
1406
- rng <- range(gdef $ bar $ value )
1407
- gdef $ bar $ value <- scales :: rescale(gdef $ bar $ value , from = rng )
1408
- gdef $ key $ .value <- scales :: rescale(gdef $ key $ .value , from = rng )
1406
+
1407
+ # Put values on a 0-1 scale
1408
+ # N.B. ggplot2 >v3.4.2 (specifically #4879) renamed bar to decor and also
1409
+ # started returning normalized values for the key field
1410
+ decor <- gdef $ decor %|| % gdef $ bar
1411
+ rng <- range(decor $ value )
1412
+ decor $ value <- scales :: rescale(decor $ value , from = rng )
1413
+ if (! " decor" %in% names(gdef )) {
1414
+ gdef $ key $ .value <- scales :: rescale(gdef $ key $ .value , from = rng )
1415
+ }
1416
+
1409
1417
vals <- lapply(gglayout [c(" xaxis" , " yaxis" )], function (ax ) {
1410
1418
if (identical(ax $ tickmode , " auto" )) ax $ ticktext else ax $ tickvals
1411
1419
})
1420
+
1412
1421
list (
1413
1422
x = vals [[1 ]][[1 ]],
1414
1423
y = vals [[2 ]][[1 ]],
@@ -1422,7 +1431,7 @@ gdef2trace <- function(gdef, theme, gglayout) {
1422
1431
# do everything on a 0-1 scale
1423
1432
marker = list (
1424
1433
color = c(0 , 1 ),
1425
- colorscale = setNames(gdef $ bar [c(" value" , " colour" )], NULL ),
1434
+ colorscale = setNames(decor [c(" value" , " colour" )], NULL ),
1426
1435
colorbar = list (
1427
1436
bgcolor = toRGB(theme $ legend.background $ fill ),
1428
1437
bordercolor = toRGB(theme $ legend.background $ colour ),
@@ -1459,3 +1468,72 @@ getAesMap <- function(plot, layer) {
1459
1468
layer $ mapping
1460
1469
}
1461
1470
}
1471
+
1472
+ # ------------------------------------------------------------------
1473
+ # Handle compatibility for changes in ggplot2 >v3.4.2 (specifically #5144),
1474
+ # which moved away from scales_transform_df(), scales_train_df(), etc
1475
+ # towards ggproto methods attached to `scales`
1476
+ # ------------------------------------------------------------------
1477
+ scales_transform_df <- function (scales , df ) {
1478
+ if (is.function(scales $ transform_df )) {
1479
+ scales $ transform_df(df )
1480
+ } else {
1481
+ ggfun(" scales_transform_df" )(df , scales = scales )
1482
+ }
1483
+ }
1484
+
1485
+ scales_train_df <- function (scales , df ) {
1486
+ if (is.function(scales $ train_df )) {
1487
+ scales $ train_df(df )
1488
+ } else {
1489
+ ggfun(" scales_train_df" )(df , scales = scales )
1490
+ }
1491
+ }
1492
+
1493
+ scales_map_df <- function (scales , df ) {
1494
+ if (is.function(scales $ map_df )) {
1495
+ scales $ map_df(df )
1496
+ } else {
1497
+ ggfun(" scales_map_df" )(df , scales = scales )
1498
+ }
1499
+ }
1500
+
1501
+ scales_add_missing <- function (plot , aesthetics ) {
1502
+ if (is.function(plot $ scales $ add_missing )) {
1503
+ plot $ scales $ add_missing(c(" x" , " y" ), plot $ plot_env )
1504
+ } else {
1505
+ ggfun(" scales_add_missing" )(plot , aesthetics , plot $ plot_env )
1506
+ }
1507
+ }
1508
+
1509
+ # -------------------------------------------------------------------------
1510
+ # Handle compatibility for changes in ggplot2 >v3.4.2 (specifically #4879),
1511
+ # which away from guides_train(), guides_merge(), guides_geom()
1512
+ # towards ggproto methods attached to `plot$guides`
1513
+ # -------------------------------------------------------------------------
1514
+ get_gdefs_ggproto <- function (scales , theme , plot , layers ) {
1515
+ guides <- plot $ guides $ setup(scales )
1516
+ guides $ train(scales , theme $ legend.direction , plot $ labels )
1517
+ if (length(guides $ guides ) > 0 ) {
1518
+ guides $ merge()
1519
+ guides $ process_layers(layers )
1520
+ }
1521
+ # Add old legend/colorbar classes to guide params so that ggplotly() code
1522
+ # can continue to work the same way it always has
1523
+ for (i in which(vapply(guides $ guides , inherits , logical (1 ), " GuideColourbar" ))) {
1524
+ guides $ params [[i ]] <- prefix_class(guides $ params [[i ]], " colorbar" )
1525
+ }
1526
+ for (i in which(vapply(guides $ guides , inherits , logical (1 ), " GuideLegend" ))) {
1527
+ guides $ params [[i ]] <- prefix_class(guides $ params [[i ]], " legend" )
1528
+ }
1529
+ guides $ params
1530
+ }
1531
+
1532
+ get_gdefs <- function (scales , theme , plot , layers ) {
1533
+ gdefs <- ggfun(" guides_train" )(scales , theme , plot $ guides , plot $ labels )
1534
+ if (length(gdefs ) > 0 ) {
1535
+ gdefs <- ggfun(" guides_merge" )(gdefs )
1536
+ gdefs <- ggfun(" guides_geom" )(gdefs , layers , plot $ mapping )
1537
+ }
1538
+ gdefs
1539
+ }
0 commit comments