|
| 1 | +#' @include class.R mark.R |
| 2 | +NULL |
| 3 | + |
| 4 | +# ---- make_mark ---- |
| 5 | +#' Create a custom mark |
| 6 | +#' |
| 7 | +#' Registers a new S7 generic + method from any ggplot2 geom function, |
| 8 | +#' making it available in the plotit pipeline. The new mark behaves |
| 9 | +#' identically to built-in marks: it supports `mapping`, `data`, |
| 10 | +#' `position`, auto-dodge, and rasterization. |
| 11 | +#' |
| 12 | +#' @param name Mark name as a string (e.g. `"mark_spoke"`). |
| 13 | +#' Should start with `"mark_"`. |
| 14 | +#' @param geom_fun A ggplot2 geom function |
| 15 | +#' (e.g. `ggplot2::geom_spoke`). |
| 16 | +#' @return Invisibly returns the registered S7 generic. |
| 17 | +#' @examples |
| 18 | +#' make_mark("mark_spoke", ggplot2::geom_spoke) |
| 19 | +#' # Now usable in pipeline: |
| 20 | +#' \dontrun{ |
| 21 | +#' df |> plotit(encode(x = x, y = y, radius = r, angle = a)) |> mark_spoke() |
| 22 | +#' } |
| 23 | +#' @export |
| 24 | +make_mark <- function(name, geom_fun) { |
| 25 | + if (!is.character(name) || length(name) != 1) { |
| 26 | + cli::cli_abort("{.arg name} must be a single string.") |
| 27 | + } |
| 28 | + if (!grepl("^mark_", name)) { |
| 29 | + cli::cli_warn( |
| 30 | + "{.arg name} should start with 'mark_', got {.val {name}}." |
| 31 | + ) |
| 32 | + } |
| 33 | + |
| 34 | + generic <- S7::new_generic( |
| 35 | + name, "plot", |
| 36 | + function(plot, mapping = NULL, data = NULL, position = NULL, ..., |
| 37 | + rasterize = FALSE, rasterize_dpi = 300, rasterize_dev = "cairo") { |
| 38 | + S7::S7_dispatch() |
| 39 | + } |
| 40 | + ) |
| 41 | + ._register_mark_method(generic, geom_fun) |
| 42 | + invisible(generic) |
| 43 | +} |
| 44 | + |
| 45 | +# ---- make_theme ---- |
| 46 | +#' Create a reusable theme preset |
| 47 | +#' |
| 48 | +#' Builds a theme function from `ggplot2::theme()` elements and |
| 49 | +#' an optional base theme. The returned function applies the theme |
| 50 | +#' to a plotit object and can be used anywhere `style()` is used. |
| 51 | +#' |
| 52 | +#' @param name Name for the theme function as a string |
| 53 | +#' (e.g. `"style_dark"`). |
| 54 | +#' @param ... Theme elements passed to [ggplot2::theme()]. |
| 55 | +#' @param base_theme A base ggplot2 theme function |
| 56 | +#' (default: [ggplot2::theme_minimal]). |
| 57 | +#' @return Invisibly returns the created function. |
| 58 | +#' @examples |
| 59 | +#' style_dark <- make_theme("style_dark", |
| 60 | +#' plot.background = ggplot2::element_rect(fill = "#1a1a1a"), |
| 61 | +#' text = ggplot2::element_text(colour = "white")) |
| 62 | +#' \dontrun{ |
| 63 | +#' plotit(iris, encode(x = Sepal.Width, y = Sepal.Length)) |> |
| 64 | +#' mark_point() |> style_dark() |
| 65 | +#' } |
| 66 | +#' @export |
| 67 | +make_theme <- function(name, ..., base_theme = ggplot2::theme_minimal) { |
| 68 | + force(name) |
| 69 | + force(base_theme) |
| 70 | + dots <- rlang::list2(...) |
| 71 | + |
| 72 | + fun <- function(plot, base_size = NULL, base_family = NULL) { |
| 73 | + thm <- base_theme( |
| 74 | + base_size = base_size %||% 11, |
| 75 | + base_family = base_family %||% "" |
| 76 | + ) + do.call(ggplot2::theme, dots) |
| 77 | + plot@gg <- plot@gg + thm |
| 78 | + plot |
| 79 | + } |
| 80 | + assign(name, fun, envir = parent.frame()) |
| 81 | + invisible(fun) |
| 82 | +} |
0 commit comments