Skip to content

Commit a5bedc7

Browse files
authored
Merge pull request #200 from teunbrand/ggplot2_3.5.0
Update for ggplot2 3.5.0
2 parents f651f45 + 5925b83 commit a5bedc7

File tree

4 files changed

+40
-17
lines changed

4 files changed

+40
-17
lines changed

R/align_plots.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ align_margin <- function(sizes, margin_to_align, greedy = TRUE) {
5353
sizes,
5454
function(x) {
5555
# find all positions of unit NULL
56-
null_idx <- grep("null", x)
56+
null_idx <- grep("null$", x)
5757
# if there are none, abort
5858
if (length(null_idx) < 1) {
5959
return(NULL)
@@ -70,7 +70,7 @@ align_margin <- function(sizes, margin_to_align, greedy = TRUE) {
7070
sizes,
7171
function(x) {
7272
# find all positions of unit NULL
73-
null_idx <- grep("null", x)
73+
null_idx <- grep("null$", x)
7474
# if there are none, abort
7575
if (length(null_idx) < 1) {
7676
return(NULL)

tests/testthat/test_align_plots.R

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ test_that("complex alignments, h, v, hv", {
4545
expect_equal(plots[[1]]$widths[1:4], plots[[2]]$widths[1:4])
4646

4747
plots <- align_plots(p1, p2, align = "v", axis = "r", greedy = FALSE) # align right
48-
expect_equal(plots[[1]]$widths[6:9], plots[[2]]$widths[14:17])
48+
n <- vapply(plots, ncol, integer(1))
49+
expect_equal(plots[[1]]$widths[n[1] - 3:0], plots[[2]]$widths[n[2] - 3:0])
4950

5051
# with greedy = TRUE, only the sums of the widths are equal
5152
plots <- align_plots(p1, p2, align = "v", axis = "l", greedy = TRUE) # align left
@@ -55,9 +56,10 @@ test_that("complex alignments, h, v, hv", {
5556
)
5657

5758
plots <- align_plots(p1, p2, align = "v", axis = "r", greedy = TRUE) # align right
59+
n <- vapply(plots, ncol, integer(1))
5860
expect_equal(
59-
grid::convertUnit(sum(plots[[1]]$widths[6:9]), "in"),
60-
grid::convertUnit(sum(plots[[2]]$widths[14:17]), "in")
61+
grid::convertUnit(sum(plots[[1]]$widths[n[1] - 3:0]), "in"),
62+
grid::convertUnit(sum(plots[[2]]$widths[n[2] - 3:0]), "in")
6163
)
6264

6365

@@ -68,15 +70,17 @@ test_that("complex alignments, h, v, hv", {
6870
expect_warning(align_plots(p1, p3, align = "h"))
6971

7072
plots <- align_plots(p1, p3, align = "h", axis = "bt", greedy = FALSE)
73+
n <- vapply(plots, nrow, integer(1))
7174
expect_equal(
72-
grid::convertUnit(plots[[1]]$heights[7:10] - plots[[2]]$heights[18:21], "cm"),
75+
grid::convertUnit(plots[[1]]$heights[n[1] - 3:0] - plots[[2]]$heights[n[2] - 3:0], "cm"),
7376
grid::unit(c(0, 0, 0, 0), "cm")
7477
)
7578

7679
# these units are only equal after we've added everything up
80+
rows <- c(panel_rows(plots[[1]])$t[1], panel_rows(plots[[2]])$t[1]) - 1
7781
expect_equal(
78-
grid::convertUnit(sum(plots[[1]]$heights[1:6]), "cm"),
79-
grid::convertUnit(sum(plots[[2]]$heights[1:7]), "cm")
82+
grid::convertUnit(sum(plots[[1]]$heights[1:rows[1]]), "cm"),
83+
grid::convertUnit(sum(plots[[2]]$heights[1:rows[2]]), "cm")
8084
)
8185
dev.off()
8286
})
@@ -106,14 +110,16 @@ test_that("complex alignments with non-plots", {
106110

107111
# because p1 has a legend and p2 doesn't, only the sums of the widths are equal for right align
108112
plots <- align_plots(p1, p2, p3, align = "v", axis = "r", greedy = FALSE) # align right
113+
n <- vapply(plots, ncol, integer(1))
109114
expect_equal(
110-
grid::convertUnit(sum(plots[[1]]$widths[6:11]), "in"),
111-
grid::convertUnit(sum(plots[[2]]$widths[14:17]), "in")
115+
grid::convertUnit(sum(plots[[1]]$widths[n[1] - 5:0]), "in"),
116+
grid::convertUnit(sum(plots[[2]]$widths[n[2] - 3:0]), "in")
112117
)
113118
plots <- align_plots(p1, NULL, p2, p3, align = "v", axis = "r", greedy = FALSE) # align right
119+
n <- vapply(plots, NCOL, integer(1))
114120
expect_equal(
115-
grid::convertUnit(sum(plots[[1]]$widths[6:11]), "in"),
116-
grid::convertUnit(sum(plots[[3]]$widths[14:17]), "in")
121+
grid::convertUnit(sum(plots[[1]]$widths[n[1] - 5:0]), "in"),
122+
grid::convertUnit(sum(plots[[3]]$widths[n[3] - 3:0]), "in")
117123
)
118124

119125
# with greedy = TRUE, only the sums of the widths are equal
@@ -129,14 +135,16 @@ test_that("complex alignments with non-plots", {
129135
)
130136

131137
plots <- align_plots(p1, p2, p3, align = "v", axis = "r", greedy = TRUE) # align right
138+
n <- vapply(plots, ncol, integer(1))
132139
expect_equal(
133-
grid::convertUnit(sum(plots[[1]]$widths[6:11]), "in"),
134-
grid::convertUnit(sum(plots[[2]]$widths[14:17]), "in")
140+
grid::convertUnit(sum(plots[[1]]$widths[n[1] - 5:0]), "in"),
141+
grid::convertUnit(sum(plots[[2]]$widths[n[2] - 3:0]), "in")
135142
)
136143
plots <- align_plots(p1, NULL, p2, align = "v", axis = "r", greedy = TRUE) # align right
144+
n <- vapply(plots, NCOL, integer(1))
137145
expect_equal(
138-
grid::convertUnit(sum(plots[[1]]$widths[6:11]), "in"),
139-
grid::convertUnit(sum(plots[[3]]$widths[14:17]), "in")
146+
grid::convertUnit(sum(plots[[1]]$widths[n[1] - 5:0]), "in"),
147+
grid::convertUnit(sum(plots[[3]]$widths[n[3] - 3:0]), "in")
140148
)
141149

142150
dev.off()

tests/testthat/test_get_legend.R

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ test_that("get legend", {
1010
expect_equal(l$name, "guide-box")
1111

1212
# return null legend if no legend
13-
expect_null(get_legend(p + theme(legend.position = "none")))
13+
# Note by Teun: It is yet unclear to me what the desired behaviour of
14+
# `get_legend()` is in the face of multiple legends.
15+
# For now, this test is conditional on ggplot2 version, but this is
16+
# more of a duct tape solution than a reflection of the intended behaviour
17+
if (utils::packageVersion("ggplot2") >= "3.5.0") {
18+
expect_s3_class(get_legend(p + theme(legend.position = "none")), "zeroGrob")
19+
} else {
20+
expect_null(get_legend(p + theme(legend.position = "none")))
21+
}
1422
})
1523

tests/testthat/test_plot_components.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ test_that("plot components", {
99
"axis-b", "spacer", "axis-r", "spacer", "xlab-t", "xlab-b", "ylab-l",
1010
"ylab-r", "guide-box", "subtitle", "title", "caption", "tag"
1111
)
12+
if (inherits(guide_legend(), "Guide")) { # proxy testing for ggplot2 3.5.0+
13+
component_names <- setdiff(component_names, c("guide-box", "tag"))
14+
component_names <- union(
15+
component_names,
16+
paste0("guide-box-", c("left", "right", "top", "bottom", "inside"))
17+
)
18+
}
1219

1320
expect_type(plot_component_names(p), "character")
1421
expect_true(all(component_names %in% plot_component_names(p)))

0 commit comments

Comments
 (0)