Quellcode durchsuchen

Add package environment for global plot settings

Currently only used for setting global text and title font sizes, but can be extended to other options that we may wish to set for more than one plot. For fonts, we now provide `set_font_size()` that can be used to set text and title sizes across all subsequent animations. Includes internal getter functions get_{text|title}_size().
pull/18/merge
Garrick Aden-Buie vor 7 Jahren
Ursprung
Commit
f536db1c0f
6 geänderte Dateien mit 74 neuen und 4 gelöschten Zeilen
  1. +1
    -0
      NAMESPACE
  2. +50
    -3
      R/plot_helpers.R
  3. +2
    -0
      R/utils.R
  4. +2
    -0
      R/zzzz-package.R
  5. +17
    -0
      man/set_font_size.Rd
  6. +2
    -1
      man/static_plot.Rd

+ 1
- 0
NAMESPACE Datei anzeigen

@@ -13,6 +13,7 @@ export(animate_setdiff)
export(animate_spread)
export(animate_union)
export(animate_union_all)
export(set_font_size)
importFrom(dplyr,anti_join)
importFrom(dplyr,arrange)
importFrom(dplyr,bind_cols)

+ 50
- 3
R/plot_helpers.R Datei anzeigen

@@ -33,9 +33,15 @@ animate_plot <- function(d, title = "", transition_length = 2, state_length = 1,
#'
#' @examples
#' NULL
static_plot <- function(d, title = "",
text_family = "Fira Sans", title_family = "Fira Mono",
text_size = 5, title_size = 17, ...) {
static_plot <- function(
d,
title = "",
text_family = "Fira Sans", title_family = "Fira Mono",
text_size = NULL, title_size = NULL,
...
) {
text_size <- get_text_size(text_size, default = 5)
title_size <- get_title_size(title_size, default = 17)

if (!".alpha" %in% names(d)) d <- d %>% mutate(.alpha = 1)
if (!".textcolor" %in% names(d))
@@ -61,3 +67,44 @@ static_plot <- function(d, title = "",
theme(plot.title = element_text(family = title_family, hjust = 0.5, size = title_size))
}

#' Set Default Text Sizes for Animation Plots
#'
#' Sets the default text sizes for the animated and static plots produced by
#' this package during the current session.
#'
#' @param text_size Font size of value labels inside the data frame squares
#' @param title_size Font size of the function call or plot title
#' @export
set_font_size <- function(text_size = NULL, title_size = NULL) {
old <- list()
if (!is.null(text_size)) old$text_size <- set_text_size(text_size)
if (!is.null(title_size)) old$title_size <- set_title_size(title_size)
invisible(old)
}

set_text_size <- function(size) {
old <- plot_settings$text_size
plot_settings$text_size <- size
invisible(old)
}

set_title_size <- function(size) {
old <- plot_settings$title_size
plot_settings$title_size <- size
invisible(old)
}

get_text_size <- function(x = NULL, default = 5) {
if (!is.null(x)) return(x)
plot_settings$text_size %||%
getFromNamespace("theme_env", "ggplot2")$current$text$size %||%
default
}

get_title_size <- function(x = NULL, default = 17) {
if (!is.null(x)) return(x)
plot_settings$title_size %||%
getFromNamespace("theme_env", "ggplot2")$current$plot.title$size %||%
default
}


+ 2
- 0
R/utils.R Datei anzeigen

@@ -1,3 +1,5 @@
`%||%` <- function(x, y) if (is.null(x)) y else x

set_text_color <- function(x, black = "#000000", white = "#FFFFFF") {
# x = color_hex
color_rgb <- col2rgb(x)

+ 2
- 0
R/zzzz-package.R Datei anzeigen

@@ -2,3 +2,5 @@
#' @importFrom dplyr mutate select filter arrange bind_rows bind_cols group_by pull slice data_frame row_number
#' @keywords internal
"_PACKAGE"

plot_settings <- new.env(parent = emptyenv())

+ 17
- 0
man/set_font_size.Rd Datei anzeigen

@@ -0,0 +1,17 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/plot_helpers.R
\name{set_font_size}
\alias{set_font_size}
\title{Set Default Text Sizes for Animation Plots}
\usage{
set_font_size(text_size = NULL, title_size = NULL)
}
\arguments{
\item{text_size}{Font size of value labels inside the data frame squares}

\item{title_size}{Font size of the function call or plot title}
}
\description{
Sets the default text sizes for the animated and static plots produced by
this package during the current session.
}

+ 2
- 1
man/static_plot.Rd Datei anzeigen

@@ -5,7 +5,8 @@
\title{Prints the tiles for a processed dataset statically}
\usage{
static_plot(d, title = "", text_family = "Fira Sans",
title_family = "Fira Mono", text_size = 5, title_size = 17, ...)
title_family = "Fira Mono", text_size = NULL, title_size = NULL,
...)
}
\arguments{
\item{d}{a processed dataset}

Laden…
Abbrechen
Speichern