Skip to content

Commit

Permalink
Add plotting functions. (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
MHenderson authored Jun 14, 2024
1 parent 9f3a9f8 commit b67790b
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 1 deletion.
5 changes: 4 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: wallis
Title: Room squares in R
Version: 0.0.0.9000
Version: 0.0.0.9001
Authors@R:
person("Matthew", "Henderson", , "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "0000-0001-7949-8208"))
Expand All @@ -10,5 +10,8 @@ Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.1
Imports:
dplyr,
ggplot2,
purrr,
tibble,
tidyr
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# Generated by roxygen2: do not edit by hand

export(plot_room_square)
42 changes: 42 additions & 0 deletions R/grid-lines.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#' Horizontal grid lines
#'
#' @param n_rows Number of rows.
#' @param n_cols Number of columns.
#'
#' @return A tibble with columns x, y, xend and yend.
horiz_lines <- function(n_rows, n_cols) {
tibble::tibble(
x = rep(0, n_rows + 1) + .5,
y = 0:n_rows + .5,
xend = rep(n_cols, n_rows + 1) + .5,
yend = 0:n_rows + .5
)
}

#' Vertical grid lines
#'
#' @param n_rows Number of rows.
#' @param n_cols Number of columns.
#'
#' @return A tibble with columns x, y, xend and yend.
vertical_lines <- function(n_rows, n_cols) {
tibble::tibble(
x = 0:n_cols + .5,
y = rep(0, n_cols + 1) + .5,
xend = 0:n_cols + .5,
yend = rep(n_rows, n_cols + 1) + .5
)
}

#' Horizontal and vertical grid lines
#'
#' @param n_rows Number of rows.
#' @param n_cols Number of columns.
#'
#' @return A tibble with columns x, y, xend and yend.
grid_lines <- function(n_rows, n_cols) {
dplyr::bind_rows(
horiz_lines(n_rows, n_cols),
vertical_lines(n_rows, n_cols)
)
}
23 changes: 23 additions & 0 deletions R/plot-room-square.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#' Plot a Room square
#'
#' @param R A Room square.
#'
#' @return A ggplot plot of Room square R
#' @export
plot_room_square <- function(R) {

n <- max(R$col) + 1

ggplot2::ggplot(data = R, ggplot2::aes(col, row)) +
ggplot2::geom_tile(data = R |> dplyr::filter(!is.na(first)), ggplot2::aes(fill = fill)) +
ggplot2::geom_segment(data = grid_lines(n - 1, n - 1), ggplot2::aes(x = x, y = y, xend = xend, yend = yend), linewidth = .1) +
ggplot2::geom_text(data = R |> dplyr::filter(!is.na(first)), ggplot2::aes(label = paste(first, second, sep = ","))) +
ggplot2::scale_y_reverse() +
ggplot2::coord_fixed() +
ggplot2::theme_void() +
ggplot2::theme(
legend.position = "none"
) +
ggplot2::scale_fill_gradient(limits = c(0, choose(n, 2)), low = "white", high = "blue")

}
19 changes: 19 additions & 0 deletions man/grid_lines.Rd

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

19 changes: 19 additions & 0 deletions man/horiz_lines.Rd

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

17 changes: 17 additions & 0 deletions man/plot_room_square.Rd

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

19 changes: 19 additions & 0 deletions man/vertical_lines.Rd

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

0 comments on commit b67790b

Please sign in to comment.