Skip to content
Draft
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
6 changes: 4 additions & 2 deletions R/plotting.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
14 changes: 14 additions & 0 deletions tests/testthat/test-plotting.R
Original file line number Diff line number Diff line change
@@ -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)
})
Loading