Skip to content

Commit a7a0652

Browse files
authored
Compatibility with incoming ggplot2 changes (#2368)
* account for changes in label setup * defaults are only computed conditionally * Use `Geom$use_defaults()` to extract defaults
1 parent 3cf17c0 commit a7a0652

File tree

5 files changed

+38
-25
lines changed

5 files changed

+38
-25
lines changed

R/ggplotly.R

+5
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,11 @@ gg2list <- function(p, width = NULL, height = NULL,
277277

278278
# Compute aesthetics to produce data with generalised variable names
279279
data <- by_layer(function(l, d) l$compute_aesthetics(d, plot))
280+
if (exists("setup_plot_labels", envir = asNamespace("ggplot2"))) {
281+
# Mirror ggplot2/#5879
282+
plot$labels <- ggfun("setup_plot_labels")(plot, layers, data)
283+
}
284+
280285

281286
# add frame to group if it exists
282287
data <- lapply(data, function(d) {

R/layers2traces.R

+28-21
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,7 @@ geom2trace.GeomPoint <- function(data, params, p) {
766766
hoveron = hover_on(data)
767767
)
768768
# fill is only relevant for pch %in% 21:25
769-
pch <- uniq(data$shape) %||% params$shape %||% GeomPoint$default_aes$shape
769+
pch <- uniq(data$shape) %||% params$shape %||% GeomPoint$use_defaults(NULL)$shape
770770
if (any(idx <- pch %in% 21:25) || any(idx <- !is.null(data[["fill_plotlyDomain"]]))) {
771771
fill_value <- aes2plotly(data, params, "fill")
772772
if (length(idx) == 1) {
@@ -866,6 +866,9 @@ geom2trace.GeomPolygon <- function(data, params, p) {
866866

867867
#' @export
868868
geom2trace.GeomBoxplot <- function(data, params, p) {
869+
# marker styling must inherit from GeomPoint$default_aes
870+
# https://github.com/hadley/ggplot2/blob/ab42c2ca81458b0cf78e3ba47ed5db21f4d0fc30/NEWS#L73-L7
871+
point_defaults <- GeomPoint$use_defaults(NULL)
869872
compact(list(
870873
x = data[["x"]],
871874
y = data[["y"]],
@@ -879,16 +882,15 @@ geom2trace.GeomBoxplot <- function(data, params, p) {
879882
aes2plotly(data, params, "fill"),
880883
aes2plotly(data, params, "alpha")
881884
),
882-
# marker styling must inherit from GeomPoint$default_aes
883-
# https://github.com/hadley/ggplot2/blob/ab42c2ca81458b0cf78e3ba47ed5db21f4d0fc30/NEWS#L73-L77
885+
# markers/points
884886
marker = list(
885-
opacity = GeomPoint$default_aes$alpha,
886-
outliercolor = toRGB(GeomPoint$default_aes$colour),
887+
opacity = point_defaults$alpha,
888+
outliercolor = toRGB(point_defaults$colour),
887889
line = list(
888-
width = mm2pixels(GeomPoint$default_aes$stroke),
889-
color = toRGB(GeomPoint$default_aes$colour)
890+
width = mm2pixels(point_defaults$stroke),
891+
color = toRGB(point_defaults$colour)
890892
),
891-
size = mm2pixels(GeomPoint$default_aes$size)
893+
size = mm2pixels(point_defaults$size)
892894
),
893895
line = list(
894896
color = aes2plotly(data, params, "colour"),
@@ -1096,21 +1098,26 @@ ribbon_dat <- function(dat) {
10961098
aes2plotly <- function(data, params, aes = "size") {
10971099
geom <- class(data)[1]
10981100

1099-
# Hack to support this geom_sf hack
1100-
# https://github.com/tidyverse/ggplot2/blob/505e4bfb/R/sf.R#L179-L187
1101-
defaults <- if (inherits(data, "GeomSf")) {
1102-
type <- if (any(grepl("[P-p]oint", class(data)))) "point" else if (any(grepl("[L-l]ine", class(data)))) "line" else ""
1103-
ggfun("default_aesthetics")(type)
1104-
} else {
1105-
geom_obj <- ggfun(geom)
1106-
# If the first class of `data` is a data.frame,
1107-
# ggfun() returns a function because ggplot2 now
1108-
# defines data.frame in it's namespace
1109-
# https://github.com/ropensci/plotly/pull/1481
1110-
if ("default_aes" %in% names(geom_obj)) geom_obj$default_aes else NULL
1101+
vals <- uniq(data[[aes]]) %||% params[[aes]]
1102+
1103+
if (is.null(vals)) {
1104+
# Hack to support this geom_sf hack
1105+
# https://github.com/tidyverse/ggplot2/blob/505e4bfb/R/sf.R#L179-L187
1106+
defaults <- if (inherits(data, "GeomSf") && exists("default_aesthetics", envir = asNamespace("ggplot2"))) {
1107+
type <- if (any(grepl("[P-p]oint", class(data)))) "point" else if (any(grepl("[L-l]ine", class(data)))) "line" else ""
1108+
ggfun("default_aesthetics")(type)
1109+
} else {
1110+
geom_obj <- ggfun(geom)
1111+
# If the first class of `data` is a data.frame,
1112+
# ggfun() returns a function because ggplot2 now
1113+
# defines data.frame in it's namespace
1114+
# https://github.com/ropensci/plotly/pull/1481
1115+
if ("default_aes" %in% names(geom_obj)) geom_obj$use_defaults(NULL) else NULL
1116+
}
1117+
vals <- defaults[[aes]]
11111118
}
1119+
vals <- vals %||% NA
11121120

1113-
vals <- uniq(data[[aes]]) %||% params[[aes]] %||% defaults[[aes]] %||% NA
11141121
converter <- switch(
11151122
aes,
11161123
size = mm2pixels,

tests/testthat/test-ggplot-area.R

+3-2
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ test_that("sanity check for geom_area", {
2626
expect_identical(L$data[[1]]$type, "scatter")
2727
expect_identical(L$data[[1]]$mode, "lines")
2828
expect_identical(L$data[[1]]$fill, "toself")
29+
area_defaults <- GeomArea$use_defaults(NULL)
2930
expect_true(
3031
L$data[[1]]$fillcolor ==
31-
toRGB(GeomArea$default_aes$fill, GeomArea$default_aes$alpha)
32+
toRGB(area_defaults$fill, area_defaults$alpha)
3233
)
3334
})
3435

@@ -40,7 +41,7 @@ test_that("transparency alpha in geom_area is converted", {
4041
expect_true(L$data[[1]]$line$color == "transparent")
4142
expect_true(
4243
L$data[[1]]$fillcolor ==
43-
toRGB(GeomArea$default_aes$fill, 0.4)
44+
toRGB(GeomArea$use_defaults(NULL)$fill, 0.4)
4445
)
4546
})
4647

tests/testthat/test-ggplot-point.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ test_that("marker color is non-transparent for open shapes", {
2929
grepl("open$", info$data[[1]]$marker$symbol)
3030
)
3131
expect_true(
32-
info$data[[1]]$marker$color == toRGB(GeomPoint$default_aes$colour)
32+
info$data[[1]]$marker$color == toRGB(GeomPoint$use_defaults(NULL)$colour)
3333
)
3434
})
3535

tests/testthat/test-ggplot-quantile.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ test_that("Basic geom_quantile() works", {
2020
expect_equivalent(tr$type, "scatter")
2121
expect_equivalent(tr$mode, "lines")
2222
expect_equivalent(
23-
tr$line$color, toRGB(GeomQuantile$default_aes[["colour"]])
23+
tr$line$color, toRGB(GeomQuantile$use_defaults(NULL)[["colour"]])
2424
)
2525
}
2626

0 commit comments

Comments
 (0)