Skip to content

Commit 2235c72

Browse files
committed
Close #2193: Accomodate breaking changes in ggplot2 3.4.0
1 parent 2db78b7 commit 2235c72

13 files changed

+66
-30
lines changed

DESCRIPTION

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,5 @@ LazyData: true
8181
RoxygenNote: 7.2.1
8282
Encoding: UTF-8
8383
Roxygen: list(markdown = TRUE)
84+
Remotes:
85+
tidyverse/ggplot2

R/ggplotly.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,12 @@ gg2list <- function(p, width = NULL, height = NULL,
305305
d
306306
})
307307

308+
layers <- lapply(layers, function(l) {
309+
l$stat$dropped_aes <- c(l$stat$dropped_aes, "x_plotlyDomain")
310+
l$stat$dropped_aes <- c(l$stat$dropped_aes, "y_plotlyDomain")
311+
l
312+
})
313+
308314
# Transform all scales
309315
data <- lapply(data, ggfun("scales_transform_df"), scales = scales)
310316

R/layers2traces.R

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -387,9 +387,13 @@ to_basic.GeomHex <- function(data, prestats_data, layout, params, p, ...) {
387387
dy <- resolution(data[["y"]], FALSE)/sqrt(3)/2 * 1.15
388388
hexC <- hexbin::hexcoords(dx, dy, n = 1)
389389
n <- nrow(data)
390-
data$size <- ifelse(data$size < 1, data$size ^ (1 / 6), data$size ^ 6)
391-
x <- rep.int(hexC[["x"]], n) * rep(data$size, each = 6) + rep(data[["x"]], each = 6)
392-
y <- rep.int(hexC[["y"]], n) * rep(data$size, each = 6) + rep(data[["y"]], each = 6)
390+
size_var <- if ("linewidth" %in% names(data)) "linewidth" else "size"
391+
size <- data[[size_var]]
392+
data[[size_var]] <- ifelse(
393+
size < 1, size ^ (1 / 6), size ^ 6
394+
)
395+
x <- rep.int(hexC[["x"]], n) * rep(data[[size_var]], each = 6) + rep(data[["x"]], each = 6)
396+
y <- rep.int(hexC[["y"]], n) * rep(data[[size_var]], each = 6) + rep(data[["y"]], each = 6)
393397
data <- data[rep(seq_len(n), each = 6), ]
394398
data[["x"]] <- x
395399
data[["y"]] <- y
@@ -558,7 +562,12 @@ to_basic.GeomSpoke <- function(data, prestats_data, layout, params, p, ...) {
558562
#' @export
559563
to_basic.GeomCrossbar <- function(data, prestats_data, layout, params, p, ...) {
560564
# from GeomCrossbar$draw_panel()
561-
middle <- base::transform(data, x = xmin, xend = xmax, yend = y, size = size * params$fatten, alpha = NA)
565+
middle <- base::transform(data, x = xmin, xend = xmax, yend = y, alpha = NA)
566+
data <- if ("linewidth" %in% names(middle)) {
567+
base::transform(data, linewidth = linewidth * params$fatten)
568+
} else {
569+
base::transform(data, size = size * params$fatten)
570+
}
562571
list(
563572
prefix_class(to_basic.GeomRect(data), "GeomCrossbar"),
564573
prefix_class(to_basic.GeomSegment(middle), "GeomCrossbar")
@@ -697,6 +706,10 @@ geom2trace.GeomBlank <- function(data, params, p) {
697706
#' @export
698707
geom2trace.GeomPath <- function(data, params, p) {
699708
data <- group2NA(data)
709+
710+
# ggplot2 >3.4.0 changed from size to linewidth for controlling line width
711+
width_var <- if ("linewidth" %in% names(data)) "linewidth" else "size"
712+
700713
L <- list(
701714
x = data[["x"]],
702715
y = data[["y"]],
@@ -710,7 +723,7 @@ geom2trace.GeomPath <- function(data, params, p) {
710723
name = if (inherits(data, "GeomSmooth")) "fitted values",
711724
line = list(
712725
# TODO: line width array? -- https://github.com/plotly/plotly.js/issues/147
713-
width = aes2plotly(data, params, "size")[1],
726+
width = aes2plotly(data, params, width_var)[1],
714727
color = toRGB(
715728
aes2plotly(data, params, "colour"),
716729
aes2plotly(data, params, "alpha")
@@ -976,12 +989,13 @@ geom2trace.default <- function(data, params, p) {
976989
# since plotly.js can't draw two polygons with different fill in a single trace
977990
split_on <- function(dat) {
978991
lookup <- list(
979-
GeomHline = c("linetype", "colour", "size"),
980-
GeomVline = c("linetype", "colour", "size"),
981-
GeomAbline = c("linetype", "colour", "size"),
982-
GeomPath = c("fill", "colour", "size"),
983-
GeomPolygon = c("fill", "colour", "size"),
992+
GeomHline = c("linetype", "colour", "size", "linewidth"),
993+
GeomVline = c("linetype", "colour", "size", "linewidth"),
994+
GeomAbline = c("linetype", "colour", "size", "linewidth"),
995+
GeomPath = c("fill", "colour", "size", "linewidth"),
996+
GeomPolygon = c("fill", "colour", "size", "linewidth"),
984997
GeomBar = "fill",
998+
# TODO: add linetype here?
985999
GeomBoxplot = c("colour", "fill", "size"),
9861000
GeomErrorbar = "colour",
9871001
GeomErrorbarh = "colour",
@@ -1093,7 +1107,8 @@ aes2plotly <- function(data, params, aes = "size") {
10931107
vals <- uniq(data[[aes]]) %||% params[[aes]] %||% defaults[[aes]] %||% NA
10941108
converter <- switch(
10951109
aes,
1096-
size = mm2pixels,
1110+
size = mm2pixels,
1111+
linewidth = mm2pixels,
10971112
stroke = mm2pixels,
10981113
colour = toRGB,
10991114
fill = toRGB,

tests/testthat/test-ggplot-abline.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ expect_traces <- function(gg, n.traces, name) {
1515
}
1616

1717
test_that("Second trace be the a-b line", {
18+
skip_if_not_installed("ggplot2", "3.4.0") # linewidth introduced in 3.4.0
19+
1820
x <- seq(0, 3.5, by = 0.5)
1921
y <- x * 0.95
2022
df <- data.frame(x, y)
2123

2224
gg <- ggplot(df) + geom_point(aes(x, y, size = x)) +
23-
geom_abline(intercept = 1.1, slope = 0.9, colour = "red", size = 4)
25+
geom_abline(intercept = 1.1, slope = 0.9, colour = "red", linewidth = 4)
2426

2527
L <- expect_traces(gg, 2, "single-abline")
2628

tests/testthat/test-ggplot-blank.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
test_that("geom_blank", {
3-
l <- ggplotly(qplot())$x
3+
qp <- expect_warning(qplot(), "deprecated")
4+
l <- ggplotly(qp)$x
45

56
expect_length(l$data, 1)
67
expect_false(l$data[[1]]$visible)

tests/testthat/test-ggplot-density.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ test_that("geom_density() respects colour aesthetic", {
5151
})
5252

5353
g <- base +
54-
geom_histogram(aes(y = ..density..), binwidth = 0.5, fill = "pink") +
54+
geom_histogram(aes(y = after_stat(density)), binwidth = 0.5, fill = "pink") +
5555
geom_density(fill = "lightblue", alpha = 0.1)
5656

57-
test_that("geom_histogram(aes(y = ..density..)) + geom_density() works", {
57+
test_that("geom_histogram(aes(y = after_stat(density))) + geom_density() works", {
5858
info <- expect_traces(g, 2, "histogram")
5959
trs <- info$data
6060
type <- unique(sapply(trs, "[[", "type"))

tests/testthat/test-ggplot-density2d.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ test_that("geom_density2d translates to path(s)", {
1414

1515
faithful$col <- factor(sample(1:20, nrow(faithful), replace = T))
1616
m <- ggplot(faithful, aes(x = eruptions, y = waiting)) +
17-
stat_density_2d(aes(fill = ..level..), geom = "polygon") +
17+
stat_density_2d(aes(fill = after_stat(level)), geom = "polygon") +
1818
geom_point(aes(colour = col)) +
1919
xlim(0.5, 6) + ylim(40, 110)
2020

tests/testthat/test-ggplot-dynamicTicks.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ test_that("Time axis inverse transforms correctly", {
9696
test_that("Inverse maps colorbar data", {
9797

9898
p <- ggplot(mpg, aes(hwy, manufacturer)) +
99-
stat_bin2d(aes(fill = ..density..), binwidth = c(3,1))
99+
stat_bin2d(aes(fill = after_stat(density)), binwidth = c(3,1))
100100

101101
l <- ggplotly(p, dynamicTicks = TRUE)$x
102102

tests/testthat/test-ggplot-histogram.R

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ test_that("geom_histogram() is a bar chart of counts with no bargap", {
2121
expect_equivalent(info$layout$barmode, "relative")
2222
})
2323

24-
test_that("geom_histogram(aes(y = ..density..)) displays a density", {
25-
info <- expect_traces(base + geom_histogram(aes(y=..density..)), 1, "density")
24+
test_that("geom_histogram(aes(y = after_stat(density))) displays a density", {
25+
info <- expect_traces(base + geom_histogram(aes(y=after_stat(density))), 1, "density")
2626
tr <- info$data[[1]]
2727
expect_identical(tr$type, "bar")
2828
#default binwidth
@@ -33,8 +33,8 @@ test_that("geom_histogram(aes(y = ..density..)) displays a density", {
3333
expect_equal(area, 1, tolerance = 0.1)
3434
})
3535

36-
test_that("geom_histogram(aes(fill = ..count..)) works", {
37-
info <- expect_traces(base + geom_histogram(aes(fill = ..count..)), 6, "fill")
36+
test_that("geom_histogram(aes(fill = after_stat(count))) works", {
37+
info <- expect_traces(base + geom_histogram(aes(fill = after_stat(count))), 6, "fill")
3838
# grab just the bar traces (there should also be a colorbar)
3939
bars <- info$data[sapply(info$data, "[[", "type") == "bar"]
4040
# each traces should have the same value of y
@@ -53,7 +53,7 @@ test_that("Histogram with fixed colour/fill works", {
5353
})
5454

5555
test_that("Specify histogram binwidth", {
56-
gg <- base + geom_histogram(aes(y=..density..), binwidth = 0.3)
56+
gg <- base + geom_histogram(aes(y=after_stat(density)), binwidth = 0.3)
5757
info <- expect_traces(gg, 1, "density-binwidth")
5858
tr <- info$data[[1]]
5959
area <- sum(tr$y) * 0.3
@@ -95,8 +95,10 @@ test_that("geom_histogram() with facets", {
9595
})
9696

9797
test_that("vline overlaid histogram", {
98+
skip_if_not_installed("ggplot2", "3.4.0") # linewidth introduced in 3.4.0
99+
98100
gg <- base + geom_histogram() +
99-
geom_vline(aes(xintercept=mean(wt)), color="red", linetype="dashed", size=1)
101+
geom_vline(aes(xintercept=mean(wt)), color="red", linetype="dashed", linewidth=1)
100102
info <- expect_traces(gg, 2, "vline")
101103
trs <- info$data
102104
type <- unique(sapply(trs, "[[", "type"))

tests/testthat/test-ggplot-hline.R

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ df <- data.frame(x, y)
55
gg <- ggplot(df) + geom_point(aes(x, y))
66

77
test_that("second trace be the hline", {
8-
p <- gg + geom_hline(yintercept = 1.1, colour = "green", size = 3)
8+
skip_if_not_installed("ggplot2", "3.4.0") # linewidth introduced in 3.4.0
9+
10+
p <- gg + geom_hline(yintercept = 1.1, colour = "green", linewidth = 3)
911

1012
L <- expect_doppelganger_built(p, "hline")
1113
expect_equivalent(length(L$data), 2)
@@ -18,7 +20,9 @@ test_that("second trace be the hline", {
1820
})
1921

2022
test_that("vector yintercept results in multiple horizontal lines", {
21-
p <- gg + geom_hline(yintercept = 1:3, colour = "red", size = 3)
23+
skip_if_not_installed("ggplot2", "3.4.0") # linewidth introduced in 3.4.0
24+
25+
p <- gg + geom_hline(yintercept = 1:3, colour = "red", linewidth = 3)
2226

2327
L <- expect_doppelganger_built(p, "hline-multiple")
2428
expect_equivalent(length(L$data), 2)
@@ -47,14 +51,16 @@ test_that("hline can be drawn over range of factors", {
4751

4852

4953
test_that("hline/vline/abline split on linetype/colour/size", {
54+
skip_if_not_installed("ggplot2", "3.4.0") # linewidth introduced in 3.4.0
55+
5056
d <- tibble::tibble(
5157
x = seq(0, 3.5, by = 0.5),
5258
y = x * 0.95
5359
)
5460
gg <- ggplot(d, aes(x, y)) +
5561
geom_point() +
5662
geom_vline(xintercept = c(2.5, 3, 3.5), linetype = 1:3) +
57-
geom_hline(yintercept = c(2.5, 3, 3.5), size = 1:3) +
63+
geom_hline(yintercept = c(2.5, 3, 3.5), linewidth = 1:3) +
5864
geom_abline(slope = -1, intercept = c(2.5, 3, 3.5), colour = 1:3)
5965

6066
l <- expect_doppelganger_built(gg, "split-hline-vline-abline")

tests/testthat/test-ggplot-polygons.R

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,11 @@ test_that("geom_polygon(aes(linetype), fill, color)", {
120120
expect_equivalent(traces.by.name[[2]]$line$dash, "dash")
121121
})
122122

123-
test_that("geom_polygon(aes(size), fill, colour)", {
123+
test_that("geom_polygon(aes(linewidth), fill, colour)", {
124+
skip_if_not_installed("ggplot2", "3.4.0") # linewidth introduced in 3.4.0
125+
124126
gg <- ggplot(poly.df) +
125-
geom_polygon(aes(x, y, size = lab), fill = "orange", colour = "black") +
127+
geom_polygon(aes(x, y, linewidth = lab), fill = "orange", colour = "black") +
126128
scale_size_manual(values = c(left = 2, right = 3))
127129
info <- expect_traces(gg, 2, "color-fill-aes-size")
128130
traces.by.name <- list()

tests/testthat/test-ggplot-tooltip.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ test_that("can hide x values in tooltip", {
4747
})
4848

4949
cars <- ggplot(mtcars, aes(mpg, factor(cyl)))
50-
p <- cars + stat_bin2d(aes(fill = ..density..), binwidth = c(3,1))
50+
p <- cars + stat_bin2d(aes(fill = after_stat(density)), binwidth = c(3,1))
5151

5252
test_that("geom_tile() displays correct info in tooltip with discrete y", {
5353
L <- expect_doppelganger_built(p, "heatmap-discrete-tooltip")

tests/testthat/test-plotly-group.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ test_that("Missing values are preserved for lines within a color variable", {
5656
m <- mtcars
5757
m$rowname <- rownames(mtcars)
5858
p <- m %>%
59-
dplyr::group_by_("rowname") %>%
59+
dplyr::group_by(rowname) %>%
6060
plot_ly(x = ~wt, y = ~mpg) %>%
6161
add_markers()
6262

0 commit comments

Comments
 (0)