diff --git a/DESCRIPTION b/DESCRIPTION index ca0c301..2972ef8 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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 diff --git a/NAMESPACE b/NAMESPACE index ffea84f..e252102 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) diff --git a/R/matrix.R b/R/matrix.R index 7873679..a67dfa5 100644 --- a/R/matrix.R +++ b/R/matrix.R @@ -1,3 +1,5 @@ +## Base matrix ---- + #' Visualize Data Inside of a Matrix #' #' Generate a graph showing the contents of a matrix. @@ -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) @@ -81,4 +86,67 @@ visualize_matrix_data <- function(data, show_indices = TRUE, ), cex = 1.2) -} \ No newline at end of file +} + +## 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 +} diff --git a/man/visualize_matrix_data.Rd b/man/draw-matrix.Rd similarity index 63% rename from man/visualize_matrix_data.Rd rename to man/draw-matrix.Rd index 6818edb..db05977 100644 --- a/man/visualize_matrix_data.Rd +++ b/man/draw-matrix.Rd @@ -2,6 +2,7 @@ % Please edit documentation in R/matrix.R \name{visualize_matrix_data} \alias{visualize_matrix_data} +\alias{visualize_matrix_data_ggplot} \title{Visualize Data Inside of a Matrix} \usage{ visualize_matrix_data( @@ -10,6 +11,13 @@ visualize_matrix_data( highlight_cells = matrix(FALSE, nrow(data), ncol(data)), highlight_color = "lemonchiffon" ) + +visualize_matrix_data_ggplot( + data, + show_indices = TRUE, + highlight_cells = matrix(FALSE, nrow(data), ncol(data)), + highlight_color = "lemonchiffon" +) } \arguments{ \item{data}{A object that has the class of \code{matrix}.} @@ -25,6 +33,24 @@ cells should be filled. Default: None.} Generate a graph showing the contents of a matrix. } \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) + +# 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) +# 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)