R/plot.R

Defines functions well_plot

Documented in well_plot

#' Plot a Plate Map
#'
#' @param data Dataframe containing data.
#' @param well Name of the column with well IDs.
#' @param value Name of the column with the values to plot.
#' @param colour Colour of the well borders.
#'
#' @importFrom rlang .data
#' @return ggplot2 object.
#' @export
#'
#' @examples
#' dat <- wellr::well_plate()[, "well"]
#' dat$value <- rnorm(96)
#' well_plot(dat, well, value)
#'
well_plot <- function(data, well, value, colour = "black") {
  data <- dplyr::mutate(
    data,
    row = wellr::well_to_row_let({{ well }}),
    row = factor(row, levels = rev(c(LETTERS, paste0("A", LETTERS)))),
    col = wellr::well_to_col_num({{ well }})
  )

  ggplot2::ggplot(data, ggplot2::aes(
    x = .data$col,
    y = .data$row,
    fill = {{ value }}
  )) +
    ggplot2::geom_tile(colour = "black") +
    ggplot2::scale_x_continuous(
      name = NULL,
      expand = ggplot2::expansion(),
      breaks = scales::breaks_width(1),
      position = "top"
    ) +
    ggplot2::scale_y_discrete(
      name = NULL,
      expand = ggplot2::expansion()
    ) +
    ggplot2::scale_fill_viridis_c() +
    ggplot2::theme_bw(base_size = 15) +
    ggplot2::coord_equal() +
    ggplot2::theme(
      panel.border = ggplot2::element_rect(colour = "black", size = 1)
    )
}
rforbiochemists/wellr documentation built on March 28, 2024, 4:26 a.m.