diff --git a/R/plotting.R b/R/plotting.R index af44f5c..02cb004 100644 --- a/R/plotting.R +++ b/R/plotting.R @@ -80,11 +80,13 @@ plot_sparse_matrix <- function(A) { A <- methods::as(A, "CsparseMatrix") A <- methods::as(A, "generalMatrix") - ggplot(summary(A), aes(x = i, y = j, fill = as.factor(x))) + + # rows of A go down the y axis and columns along the x axis, as in + # plot_dense_matrix() + ggplot(summary(A), aes(x = j, y = i, fill = as.factor(x))) + geom_tile() + scale_fill_grey() + scale_y_reverse() + - expand_limits(x = nrow(A), y = ncol(A)) + + expand_limits(x = ncol(A), y = nrow(A)) + theme_void() + labs( fill = "Edges" diff --git a/tests/testthat/test-plotting.R b/tests/testthat/test-plotting.R new file mode 100644 index 0000000..69f3a79 --- /dev/null +++ b/tests/testthat/test-plotting.R @@ -0,0 +1,14 @@ +test_that("plot_sparse_matrix() has the same orientation as plot_dense_matrix()", { + # a single entry in row 1, column 3 of a 2 x 4 matrix + A <- Matrix::sparseMatrix(i = 1, j = 3, x = 1, dims = c(2, 4)) + + sparse <- ggplot2::layer_data(plot_sparse_matrix(A)) + dense <- ggplot2::layer_data(plot_dense_matrix(as.matrix(A))) + dense <- dense[dense$fill == max(dense$fill), ] + + # columns along the x axis, rows down the (reversed) y axis + expect_equal(sparse$x, 3) + expect_equal(sparse$y, -1) + expect_equal(sparse$x, dense$x) + expect_equal(sparse$y, dense$y) +})