Skip to content

Commit

Permalink
Add ggplot2 as an option for the graphs...
Browse files Browse the repository at this point in the history
  • Loading branch information
coatless committed Jan 10, 2024
1 parent 7ba4346 commit f789b02
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 2 deletions.
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ Authors@R: c(
Description: Visualize the contents of different 'R' data structures.
URL: https://github.com/coatless-rpkg/drawr, http://r-pkg.thecoatlessprofessor.com/drawr/
BugReports: https://github.com/coatless-rpkg/drawr/issues
Depends: R (>= 4.3.0)
Depends: R (>= 4.2.0)
License: GPL (>= 2)
Encoding: UTF-8
RoxygenNote: 7.2.3
Roxygen: list(markdown = TRUE)
Imports:
graphics
Suggests:
ggplot2
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by roxygen2: do not edit by hand

export(visualize_matrix_data)
export(visualize_matrix_data_ggplot)
importFrom(graphics,rect)
importFrom(graphics,text)
70 changes: 69 additions & 1 deletion R/matrix.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
## Base matrix ----

#' Visualize Data Inside of a Matrix
#'
#' Generate a graph showing the contents of a matrix.
Expand All @@ -9,8 +11,11 @@
#' @param highlight_color Color to use to fill the background of a cell.
#'
#' @importFrom graphics rect text
#' @rdname draw-matrix
#' @export
#' @examples
#' # Base graphics
#'
#' # Visualize a 3x3
#' mat_3x3 = matrix(c(10, 200, -30, 40, 500, 30, 90, -55, 10), ncol = 3)
#' visualize_matrix_data(mat_3x3)
Expand Down Expand Up @@ -81,4 +86,67 @@ visualize_matrix_data <- function(data, show_indices = TRUE,
),
cex = 1.2)

}
}

## ggplot2 matrix ----

#' @rdname draw-matrix
#' @export
#' @examples
#' # ggplot2 graphics
#' # Visualize a 3x3
#' mat_3x3 = matrix(c(10, 200, -30, 40, 500, 30, 90, -55, 10), ncol = 3)
#' visualize_matrix_data(mat_3x3)
#'
#' # Disable the indices
#' visualize_matrix_data(mat_3x3, show_indices = 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)
#' visualize_matrix_data(mat_2x2, highlight_cells = mat_2x2_mask)
#'
#' # Highlight values above 5
#' mat_3x5 = matrix(rnorm(15, 5, 2), ncol = 5)
#' visualize_matrix_data(mat_3x5, highlight_cells = mat_3x5 > 2)
visualize_matrix_data_ggplot <- function(data, show_indices = TRUE,
highlight_cells = matrix(FALSE, nrow(data), ncol(data)),
highlight_color = "lemonchiffon") {

if(!requireNamespace("ggplot2", quietly = TRUE)) {
message("Please make sure `ggplot2` is installed to use this function.")
}

if (!is.matrix(data)) {
message("Please double-check the data supplied is of a matrix type.")
}

nrow <- nrow(data)
ncol <- ncol(data)

# Create a data frame for ggplot
df <- expand.grid(row = nrow:1, col = 1:ncol)
df$highlight <- as.vector(highlight_cells)
df$point_contents <- as.vector(data)

# TODO: fix no visible binding for global variable

g <- ggplot2::ggplot(df) +
ggplot2::aes(x = col, y = row) +
ggplot2::geom_tile(ggplot2::aes(fill = df$highlight), color = "black", width = 1, height = 1) +
ggplot2::scale_fill_manual(values = c("white", highlight_color)) +
ggplot2::geom_text(
ggplot2::aes(
label = ifelse(!is.na(df$point_contents), as.character(df$point_contents), "NA")),
size = 5, color = "black") +
ggplot2::labs(
title = paste("Data Object: ", deparse(substitute(data))),
subtitle = paste0(
"Dimensions: ", paste(nrow, "rows", ncol, "columns"), " | ",
"Data Type: ", paste0(class(data), collapse=", "))
) +
ggplot2::theme_void() +
ggplot2::theme(legend.position = "none")

g
}
26 changes: 26 additions & 0 deletions man/visualize_matrix_data.Rd → 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 f789b02

Please sign in to comment.