R/visualize.R

Defines functions plot.board

Documented in plot.board

#' plot.board
#'
#' @param boardobj a board
#'
#' @import ggplot2
#' @import tidyr
#' @return plots the board as a side effect
#' @export
#'
#' @examples board_example <- board(matrix(c(0,1,1,1,0,
#' 0,1,1,0,1,
#' 0,0,1,0,0,
#' 0,0,0,2,2,
#' 2,2,2,2,0), 5, 5))
#'
#' ^note the matrix will be the transpose of what you see here (due to byrow = F)
#' plot(board_example)
plot.board = function(boardobj){
  n <- attr(board, "n")
  board_df <- tidyr::gather(data.frame(row = 1:nrow(boardobj), boardobj),
                            key = "column", value = "value", -row)
  board_df$column <- as.numeric(substr(board_df$column, 2, nchar(board_df$column)))
  board_df$value = as.factor(board_df$value)
  colors = c("black", "white", "lightblue3")
  names(colors) = c(0,1,2)
  ggplot(data = board_df, aes(x = column, y =max(row) - row, fill = value)) +
    scale_fill_manual(values = colors ) + theme_void() + theme(legend.position = "none") +
    geom_tile(color = "grey", linetype = "dashed") + labs(title = paste("Size:", as.character(nrow(boardobj))))
}
Achowdh1/percolate documentation built on Oct. 30, 2019, 4:09 a.m.