diff --git a/R/plot.R b/R/plot.R index 518c17c581..f79a013801 100644 --- a/R/plot.R +++ b/R/plot.R @@ -142,6 +142,11 @@ plot.igraph <- function( } layout <- i.postprocess.layout(params("plot", "layout")) + if (nrow(layout) != vc) { + cli::cli_abort(c( + "The layout has {nrow(layout)} rows, but the graph has {vc} vertices.", + "i" = "It is recommended to store the layout as x and y vertex attributes and not as a matrix graph attribute.")) + } margin <- params("plot", "margin") margin <- rep(margin, length.out = 4) rescale <- params("plot", "rescale") diff --git a/tests/testthat/_snaps/plot.md b/tests/testthat/_snaps/plot.md new file mode 100644 index 0000000000..4d902f54da --- /dev/null +++ b/tests/testthat/_snaps/plot.md @@ -0,0 +1,9 @@ +# layout as graph attribute error works + + Code + plot(g) + Condition + Error in `plot()`: + ! The layout has 5 rows, but the graph has 10 vertices. + i It is recommended to store the layout as x and y vertex attributes and not as a matrix graph attribute. + diff --git a/tests/testthat/test-plot.R b/tests/testthat/test-plot.R index 7e2e6b6696..30c06d9412 100644 --- a/tests/testthat/test-plot.R +++ b/tests/testthat/test-plot.R @@ -138,6 +138,15 @@ test_that("Edges stop at outside of rectangle node", { vdiffr::expect_doppelganger("rectangle-edges", rectangle_edges) }) + +test_that("layout as graph attribute error works", { + g <- make_full_graph(10) + g$layout <- layout_in_circle(g)[1:5, ] + expect_snapshot(error = TRUE, { + plot(g) + }) +}) + test_that("Multi loops are arranged correctly", { skip_if_not_installed("vdiffr")