diff --git a/R/plot.R b/R/plot.R index f79a013801a..855fdec3ff3 100644 --- a/R/plot.R +++ b/R/plot.R @@ -89,8 +89,8 @@ plot.igraph <- function( # SPECIFIC: ##################################### axes = FALSE, add = FALSE, - xlim = c(-1, 1), - ylim = c(-1, 1), + xlim = NULL, + ylim = NULL, mark.groups = list(), mark.shape = 1 / 2, mark.col = rainbow(length(mark.groups), alpha = 0.3), @@ -170,6 +170,12 @@ plot.igraph <- function( ################################################################ ## create the plot if (rescale) { + if (is.null(xlim)) { + xlim <- c(-1, 1) + } + if (is.null(ylim)) { + ylim <- c(-1, 1) + } layout <- norm_coords(layout, -1, 1, -1, 1) fact <- (1 - vertex.size.scaling) maxv <- 1 / 200 * max(vertex.size) @@ -182,6 +188,13 @@ plot.igraph <- function( ylim[1] - margin[1] - fact * maxv, ylim[2] + margin[3] + fact * maxv ) + } else { + if (is.null(xlim)) { + xlim <- range(layout[, 1]) + c(-margin[2], margin[4]) + } + if (is.null(ylim)) { + ylim <- range(layout[, 2]) + c(-margin[1], margin[3]) + } } if (!add) { plot( diff --git a/man/plot.igraph.Rd b/man/plot.igraph.Rd index c1ee1232a98..95d371b8999 100644 --- a/man/plot.igraph.Rd +++ b/man/plot.igraph.Rd @@ -9,8 +9,8 @@ x, axes = FALSE, add = FALSE, - xlim = c(-1, 1), - ylim = c(-1, 1), + xlim = NULL, + ylim = NULL, mark.groups = list(), mark.shape = 1/2, mark.col = rainbow(length(mark.groups), alpha = 0.3), diff --git a/tests/testthat/_snaps/plot/rescale-coords.svg b/tests/testthat/_snaps/plot/rescale-coords.svg new file mode 100644 index 00000000000..6f30d7e9bd6 --- /dev/null +++ b/tests/testthat/_snaps/plot/rescale-coords.svg @@ -0,0 +1,382 @@ + + + + + + + + + + + + + + + + + + +-1.0 +-0.5 +0.0 +0.5 +1.0 + + + + + + + + +-3 +-2 +-1 +0 +1 +2 +3 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +1 +2 +3 +4 +5 +6 +7 +8 +9 +10 +11 + + + + + + + + + + + + + + + +-4 +-2 +0 +2 +4 + + + + + + +-10 +-5 +0 +5 +10 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +1 +2 +3 +4 +5 +6 +7 +8 +9 +10 +11 + + + + + + + + + + + + + + + +-4 +-2 +0 +2 +4 + + + + + + +-10 +-5 +0 +5 +10 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +1 +2 +3 +4 +5 +6 +7 +8 +9 +10 +11 + + diff --git a/tests/testthat/test-plot.R b/tests/testthat/test-plot.R index 30c06d94121..1467da59594 100644 --- a/tests/testthat/test-plot.R +++ b/tests/testthat/test-plot.R @@ -233,3 +233,35 @@ test_that("mark border linewidth", { vdiffr::expect_doppelganger("mark-border-lwd", mark_border_lwd) }) + +test_that("plot rescales correctly", { + skip_if_not_installed("vdiffr") + rescale_coords <- function() { + n <- 11 + a <- seq(0, 2 * pi, length.out = n + 1)[-1] + x <- 5 * cos(a) + y <- 3 * sin(a) + L <- matrix(c(x, y), ncol = 2) + + G <- make_full_graph(n) |> + set_vertex_attr("x", value = x) |> + set_vertex_attr("y", value = y) + + withr::with_par( + list(mfrow = c(1, 3)), + code = { + plot(G, layout = layout_nicely(G), axes = TRUE) + plot(G, layout = L, rescale = FALSE, axes = TRUE) + plot( + G, + xlim = range(V(G)$x), + ylim = range(V(G)$y), + rescale = FALSE, + asp = 1, + axes = TRUE + ) + } + ) + } + vdiffr::expect_doppelganger("rescale-coords", rescale_coords) +})