Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update for ggplot2 3.5.0 #200

Merged
merged 5 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions R/align_plots.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ align_margin <- function(sizes, margin_to_align, greedy = TRUE) {
sizes,
function(x) {
# find all positions of unit NULL
null_idx <- grep("null", x)
null_idx <- grep("null$", x)
# if there are none, abort
if (length(null_idx) < 1) {
return(NULL)
Expand All @@ -70,7 +70,7 @@ align_margin <- function(sizes, margin_to_align, greedy = TRUE) {
sizes,
function(x) {
# find all positions of unit NULL
null_idx <- grep("null", x)
null_idx <- grep("null$", x)
# if there are none, abort
if (length(null_idx) < 1) {
return(NULL)
Expand Down
36 changes: 22 additions & 14 deletions tests/testthat/test_align_plots.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ test_that("complex alignments, h, v, hv", {
expect_equal(plots[[1]]$widths[1:4], plots[[2]]$widths[1:4])

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

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

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


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

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

# these units are only equal after we've added everything up
rows <- c(panel_rows(plots[[1]])$t[1], panel_rows(plots[[2]])$t[1]) - 1
expect_equal(
grid::convertUnit(sum(plots[[1]]$heights[1:6]), "cm"),
grid::convertUnit(sum(plots[[2]]$heights[1:7]), "cm")
grid::convertUnit(sum(plots[[1]]$heights[1:rows[1]]), "cm"),
grid::convertUnit(sum(plots[[2]]$heights[1:rows[2]]), "cm")
)
dev.off()
})
Expand Down Expand Up @@ -106,14 +110,16 @@ test_that("complex alignments with non-plots", {

# because p1 has a legend and p2 doesn't, only the sums of the widths are equal for right align
plots <- align_plots(p1, p2, p3, align = "v", axis = "r", greedy = FALSE) # align right
n <- vapply(plots, ncol, integer(1))
expect_equal(
grid::convertUnit(sum(plots[[1]]$widths[6:11]), "in"),
grid::convertUnit(sum(plots[[2]]$widths[14:17]), "in")
grid::convertUnit(sum(plots[[1]]$widths[n[1] - 5:0]), "in"),
grid::convertUnit(sum(plots[[2]]$widths[n[2] - 3:0]), "in")
)
plots <- align_plots(p1, NULL, p2, p3, align = "v", axis = "r", greedy = FALSE) # align right
n <- vapply(plots, NCOL, integer(1))
expect_equal(
grid::convertUnit(sum(plots[[1]]$widths[6:11]), "in"),
grid::convertUnit(sum(plots[[3]]$widths[14:17]), "in")
grid::convertUnit(sum(plots[[1]]$widths[n[1] - 5:0]), "in"),
grid::convertUnit(sum(plots[[3]]$widths[n[3] - 3:0]), "in")
)

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

plots <- align_plots(p1, p2, p3, align = "v", axis = "r", greedy = TRUE) # align right
n <- vapply(plots, ncol, integer(1))
expect_equal(
grid::convertUnit(sum(plots[[1]]$widths[6:11]), "in"),
grid::convertUnit(sum(plots[[2]]$widths[14:17]), "in")
grid::convertUnit(sum(plots[[1]]$widths[n[1] - 5:0]), "in"),
grid::convertUnit(sum(plots[[2]]$widths[n[2] - 3:0]), "in")
)
plots <- align_plots(p1, NULL, p2, align = "v", axis = "r", greedy = TRUE) # align right
n <- vapply(plots, NCOL, integer(1))
expect_equal(
grid::convertUnit(sum(plots[[1]]$widths[6:11]), "in"),
grid::convertUnit(sum(plots[[3]]$widths[14:17]), "in")
grid::convertUnit(sum(plots[[1]]$widths[n[1] - 5:0]), "in"),
grid::convertUnit(sum(plots[[3]]$widths[n[3] - 3:0]), "in")
)

dev.off()
Expand Down
10 changes: 9 additions & 1 deletion tests/testthat/test_get_legend.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ test_that("get legend", {
expect_equal(l$name, "guide-box")

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

7 changes: 7 additions & 0 deletions tests/testthat/test_plot_components.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ test_that("plot components", {
"axis-b", "spacer", "axis-r", "spacer", "xlab-t", "xlab-b", "ylab-l",
"ylab-r", "guide-box", "subtitle", "title", "caption", "tag"
)
if (inherits(guide_legend(), "Guide")) { # proxy testing for ggplot2 3.5.0+
component_names <- setdiff(component_names, c("guide-box", "tag"))
component_names <- union(
component_names,
paste0("guide-box-", c("left", "right", "top", "bottom", "inside"))
)
}

expect_type(plot_component_names(p), "character")
expect_true(all(component_names %in% plot_component_names(p)))
Expand Down