Skip to content

Commit

Permalink
Clean up vectors
Browse files Browse the repository at this point in the history
Expose title
  • Loading branch information
coatless committed Jan 15, 2024
1 parent 180a2c9 commit ed713b3
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 37 deletions.
78 changes: 44 additions & 34 deletions R/vector.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
#' Generate a graph showing the contents of a Vector
#'
#' @param data A object that has the class of `vector`.
#' @param show_cell_indices Display cell indices inside the matrix cell, e.g. `[i]`. Default: `FALSE`
#' @param layout Orientation of the vector. Default: `"vertical"`.
#' @param show_indices Display data indices either: `"inside"`, `"outside"`, or `"none"`
#' the vector cell, e.g. `[i]`. Default: `"none"`
#' @param highlight_area Vector 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.
#'
#' @param graph_title Title to appear in the upper left hand corner of the graph.
#' @param graph_subtitle Subtitle to appear immediately under the graph title
#' in the upper left hand side of the graph.
#'
#' @importFrom graphics rect text mtext par plot.new plot.window
#' @rdname draw-vector
#' @export
Expand All @@ -23,26 +27,34 @@
draw_vector <- function(
data,
layout = c("vertical", "horizontal"),
show_cell_indices = FALSE,
show_indices = c("none", "inside", "outside"),
highlight_area = rep(FALSE, length(data)),
highlight_color = "lemonchiffon"
highlight_color = "lemonchiffon",
graph_title = paste0("Data Object: ", deparse(substitute(data))),
graph_subtitle = paste0(
"Length: ", paste(length(data), "elements"), " | ",
"Data Type: ", paste0(class(data), collapse=", ")
)
) {

if (!is.vector(data)) {
stop("Please double check the data supplied is of a `vector` type.")
}

layout <- match.arg(layout)
show_indices <- match.arg(show_indices)

nrow <- length(data)
ncol <- 1
n_elem <- length(data)

is_row_layout <- layout == "horizontal"
is_column_layout <- !is_row_layout

if (is_row_layout) {
swap <- ncol
ncol <- nrow
nrow <- swap
if(is_row_layout) {
n_row <- 1
n_col <- n_elem
} else {
n_row <- n_elem
n_col <- 1
}

# Remove exterior margin
Expand All @@ -51,64 +63,62 @@ draw_vector <- function(
# Initialize plot at the origin
plot.new()
plot.window(
xlim = c(0, ncol + 1), ylim = c(-.1, nrow + .2)
xlim = c(0, n_col + 1), ylim = c(-.1, n_row + .1)
)

# TODO: Re-write to remove for loops.
for (i in seq_len(nrow)) {
for (j in seq_len(ncol)) {
# This is a messay backport ...
for (i in seq_len(n_row)) {
for (j in seq_len(n_col)) {

position <- max(i,j)

# Draw rectangle around each cell
rect(
# xleft, ybottom
j - 0.5, nrow - i + 1,
j - 0.5, n_row - i + 1,
# xright, ytop
j + 0.5, nrow - i,
col = ifelse(highlight_area[i], highlight_color, "white"),
j + 0.5, n_row - i,
col = ifelse(highlight_area[position], highlight_color, "white"),
border = "black"
)

# Differentiate between missing and present values
point_contents <- data[i]
point_contents <- data[position]
if (is.finite(point_contents) ) {
text(j, nrow - i + 0.5, data[i], cex = 1.25, col = "black")
text(j, n_row - i + 0.5, point_contents, cex = 1.25, col = "black")
} else if( is.infinite(point_contents) || is.nan(point_contents) ) {
text(j, nrow - i + 0.5, data[i], cex = 1.25, col = "blue")
text(j, n_row - i + 0.5, point_contents, cex = 1.25, col = "blue")
} else {
# NA
text(j, nrow - i + 0.5, "NA", cex = 1.25, col = "red")
text(j, n_row - i + 0.5, "NA", cex = 1.25, col = "red")
}

# Label each entry inside of the matrix
if (show_cell_indices) {
text(j, nrow - i + 0.3, paste("[", i, "]", sep = ""), cex = .9, col = "grey")
if (show_indices == "inside") {
text(j, n_row - i + 0.3, paste("[", position, "]", sep = ""), cex = .9, col = "grey")
}
}
}

# Add row indices to the left
if (show_cell_indices && is_row_layout) {
for (i in seq_len(nrow)) {
text(0.25, nrow - i + 0.5, paste("[", i, "]", sep = ""), cex = .95, col = "grey")
if (show_indices == "outside" && is_column_layout) {
for (i in seq_len(n_row)) {
text(0.25, n_row - i + 0.5, paste("[", i, "]", sep = ""), cex = .95, col = "grey")
}
}

# Add column indices to the top
if (show_cell_indices && !is_row_layout ) {
for (j in seq_len(ncol)) {
text(j, nrow + 0.15, paste("[", j, "]", sep = ""), cex = .95, col = "grey")
if (show_indices == "outside" && is_row_layout ) {
for (j in seq_len(n_col)) {
text(j, n_row + 0.1, paste("[", j, "]", sep = ""), cex = .95, col = "grey")
}
}

# Draw a rectangle around all cells in the matrix
rect(0.5, nrow, ncol + 0.5 , 0, border = "black", lwd = 2)
rect(0.5, n_row, n_col + 0.5 , 0, border = "black", lwd = 2)

# Add title with data object name, dimensions, and data type
graph_title = paste0("Data Object: ", deparse(substitute(data)))
graph_subtitle = paste0(
"Dimensions: ", paste(nrow, "rows", ncol, "columns"), " | ",
"Data Type: ", paste0(class(data), collapse=", ")
)

# Left-align title inside of the margins of text
mtext(
Expand Down
15 changes: 12 additions & 3 deletions man/draw-vector.Rd

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

0 comments on commit ed713b3

Please sign in to comment.