Skip to content

Commit

Permalink
highlight_cells -> highlight_area
Browse files Browse the repository at this point in the history
  • Loading branch information
coatless committed Jan 11, 2024
1 parent bb95c19 commit e789149
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 36 deletions.
39 changes: 20 additions & 19 deletions R/matrix.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
#' Generate a graph showing the contents of a matrix.
#'
#' @param data A object that has the class of `matrix`.
#' @param show_cell_indices Display cell indices inside the matrix cell, e.g. `[i, j]`. Default: `TRUE`
#' @param show_cell_indices Display cell indices inside the matrix cell, e.g. `[i, j]`. Default: `FALSE`
#' @param show_row_indices Display row indices next to matrix row, e.g. `[i, ]`. Default: `FALSE`
#' @param show_column_indices Display column indices next to matrix column, e.g. `[, j]`. Default: `FALSE`
#' @param highlight_cells Matrix of logical values that provide a mask for what
#' @param highlight_area Matrix of logical values that provide a mask for what
#' cells should be filled. Default: None.
#' @param highlight_color Color to use to fill the background of a cell.
#'
Expand All @@ -22,23 +22,24 @@
#' mat_3x3 = matrix(c(10, 200, -30, 40, 500, 30, 90, -55, 10), ncol = 3)
#' draw_matrix(mat_3x3)
#'
#' # Disable the indices
#' draw_matrix(mat_3x3, show_cell_indices = FALSE)
#' # Show the indices
#' draw_matrix(mat_3x3, show_cell_indices = TRUE)
#'
#' # Highlight a row
#' mat_2x2 = matrix(c(1, 2, 3, 4), nrow = 2)
#' mat_2x2_mask = matrix(c(TRUE, TRUE, FALSE, FALSE), nrow = 2)
#' draw_matrix(mat_2x2, highlight_cells = mat_2x2_mask)
#' mat_4x4 = matrix(seq_len(16), nrow = 4)
#' draw_matrix(
#' mat_4x4,
#' show_row_indices = TRUE, highlight_area = highlight_rows(mat_4x4, rows = 1))
#'
#' # Highlight values above 5
#' mat_3x5 = matrix(rnorm(15, 5, 2), ncol = 5)
#' draw_matrix(mat_3x5, highlight_cells = mat_3x5 > 2)
#' mat_2x4 = matrix(round(rnorm(16, 5, 2), 2), ncol = 4)
#' draw_matrix(mat_2x4, highlight_area = mat_2x4 > 2)
draw_matrix <- function(
data,
show_cell_indices = TRUE,
show_cell_indices = FALSE,
show_row_indices = FALSE,
show_column_indices = FALSE,
highlight_cells = matrix(FALSE, nrow(data), ncol(data)),
highlight_area = matrix(FALSE, nrow(data), ncol(data)),
highlight_color = "lemonchiffon"
) {

Expand Down Expand Up @@ -68,7 +69,7 @@ draw_matrix <- function(
j - 0.5, nrow - i + 1,
# xright, ytop
j + 0.5, nrow - i,
col = ifelse(highlight_cells[i, j], highlight_color, "white"),
col = ifelse(highlight_area[i, j], highlight_color, "white"),
border = "black"
)

Expand Down Expand Up @@ -138,22 +139,22 @@ draw_matrix <- function(
#' gdraw_matrix(mat_3x3)
#'
#' # View the matrix without indices present
#' gdraw_matrix(mat_3x3, show_cell_indices = FALSE)
#' gdraw_matrix(mat_3x3, highlight_area = FALSE)
#'
#' # Highlight a row
#' mat_2x2 = matrix(c(1, 2, 3, 4), nrow = 2)
#' mat_2x2_mask = matrix(c(TRUE, TRUE, FALSE, FALSE), nrow = 2)
#' gdraw_matrix(mat_2x2, highlight_cells = mat_2x2_mask)
#' gdraw_matrix(mat_2x2, highlight_area = mat_2x2_mask)
#'
#' # Highlight values above 5
#' mat_3x5 = matrix(rnorm(15, 5, 2), ncol = 5)
#' gdraw_matrix(mat_3x5, highlight_cells = mat_3x5 > 2)
#' mat_3x5 = matrix(round(rnorm(15, 5, 2), 2), ncol = 5)
#' gdraw_matrix(mat_3x5, highlight_area = mat_3x5 > 2)
gdraw_matrix <- function(
data,
show_cell_indices = TRUE,
show_cell_indices = FALSE,
show_row_indices = FALSE,
show_column_indices = FALSE,
highlight_cells = matrix(FALSE, nrow(data), ncol(data)),
highlight_area = matrix(FALSE, nrow(data), ncol(data)),
highlight_color = "lemonchiffon"
) {

Expand All @@ -176,7 +177,7 @@ gdraw_matrix <- function(

# Create a data frame for ggplot
df <- expand.grid(row = rev(row_ind), col = col_ind)
df$highlight <- as.vector(highlight_cells)
df$highlight <- as.vector(highlight_area)
df$value <- as.vector(data)

# TODO: fix no visible binding for global variable
Expand Down
35 changes: 18 additions & 17 deletions man/draw-matrix.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e789149

Please sign in to comment.