library(testthat)
library(dplyr)
library(ggplot2)
library(readr)
library(cli)
# Load already included functions
pkgload::load_all(export_all = FALSE)

Create a theme function in correspondance with the Figma maquette

library(ggplot2)

my_awesome_theme <- get_figma_file_content(file_key = "wRqIvMmymzSPuj0sEhnORb",
                                  acess_token = Sys.getenv("FIGMA_TOKEN")) %>%
  extract_ggplot_theme() %>% 
  create_theme_fun()

# Toy plot 1
#' \dontrun{
ggplot(data = iris) + 
  aes(x = Sepal.Width, fill = Species) + 
  geom_density() + 
  labs(title = "Sepal width of several species of iris",
       subtitle = "This plot respects the graphic design system defined in Figma",
       x = "Sepal width",
       y = "Density", 
       color = "Iris species") +
  my_awesome_theme()
#' }

# Toy plot 2
#' \dontrun{
ggplot(data = iris) + 
  aes(x = Sepal.Width, fill = Species) + 
  geom_density() + 
  labs(title = "Sepal width of several species of iris",
       subtitle = "This plot respects the graphic design system defined in Figma",
       x = "Sepal width",
       y = "Density", 
       color = "Iris species") +
  my_awesome_theme(legend.position = "bottom")
#' }

# Toy plot 3
#' \dontrun{
ggplot(data = iris) + 
  aes(x = Sepal.Length, y = Sepal.Width, color = Species) + 
  geom_point() + 
  labs(title = "Relationship between sepal length and sepal width of several species of iris",
       subtitle = "This plot respects the graphic design system defined in Figma",
       x = "Sepal length",
       y = "Sepal width", 
       color = "Iris species") +
  my_awesome_theme()
#' }

# Toy plot 4
#' \dontrun{
ggplot(data = iris) + 
  aes(x = Sepal.Length, y = Sepal.Width) + 
  geom_point() + 
  labs(title = "Relationship between sepal length and sepal width of several species of iris",
       subtitle = "This plot respects the graphic design system defined in Figma",
       x = "Sepal length",
       y = "Sepal width") +
  facet_grid(cols = vars(Species)) +
  my_awesome_theme()
#' }

Details of the functions

Create a theme function by using the {ggplot2} elements extracted from the Figma file

#' Create a theme functions by using the {ggplot2} elements extracted from the Figma file
#'
#' @param .data Tibble. {ggplot2} elements as created by \code{architekter::\link{extract_ggplot_theme}()}
#' 
#' @importFrom ggplot2 theme element_rect element_line element_text
#' @importFrom dplyr filter pull
#' @importFrom cli cli_alert_success
#'
#' @return A {ggplot2} theme function.
#' @export
create_theme_fun <- function(.data) {

  ad_hoc_theme_fun <- function(...) {
    theme(
      panel.background = element_rect(
        size = .data %>% filter(element_name == "panel_background") %>% pull(size),
        color = .data %>% filter(element_name == "panel_background") %>% pull(color),
        fill = .data %>% filter(element_name == "panel_background") %>% pull(fill),
        linetype = .data %>% filter(element_name == "panel_background") %>% pull(linetype)
      ),
      panel.grid.major = element_line(
        size = .data %>% filter(element_name == "panel_grid") %>% pull(size) / 10,
        color = .data %>% filter(element_name == "panel_grid") %>% pull(color),
        linetype = .data %>% filter(element_name == "panel_grid") %>% pull(linetype)
      ),
      panel.grid.minor = element_line(
        size = .data %>% filter(element_name == "panel_grid") %>% pull(size) / 10,
        color = .data %>% filter(element_name == "panel_grid") %>% pull(color),
        linetype = .data %>% filter(element_name == "panel_grid") %>% pull(linetype)
      ),
      plot.title = element_text(
        lineheight = .data %>% filter(element_name == "plot_title") %>% pull(lineheight),
        family = .data %>% filter(element_name == "plot_title") %>% pull(family),
        size = .data %>% filter(element_name == "plot_title") %>% pull(size) / 1.5,
        color = .data %>% filter(element_name == "plot_title") %>% pull(color),
      ),
      plot.subtitle = element_text(
        lineheight = .data %>% filter(element_name == "plot_subtitle") %>% pull(lineheight),
        family = .data %>% filter(element_name == "plot_subtitle") %>% pull(family),
        size = .data %>% filter(element_name == "plot_subtitle") %>% pull(size) / 1.5,
        color = .data %>% filter(element_name == "plot_subtitle") %>% pull(color),
      ),
      legend.title = element_text(
        lineheight = .data %>% filter(element_name == "legend_title") %>% pull(lineheight),
        family = .data %>% filter(element_name == "legend_title") %>% pull(family),
        size = .data %>% filter(element_name == "legend_title") %>% pull(size) / 1.5,
        color = .data %>% filter(element_name == "legend_title") %>% pull(color),
      ),
      legend.text = element_text(
        lineheight = .data %>% filter(element_name == "legend_text") %>% pull(lineheight),
        family = .data %>% filter(element_name == "legend_text") %>% pull(family),
        size = .data %>% filter(element_name == "legend_text") %>% pull(size) / 1.5,
        color = .data %>% filter(element_name == "legend_text") %>% pull(color),
      ),
      axis.title = element_text(
        lineheight = .data %>% filter(element_name == "axis_title") %>% pull(lineheight),
        family = .data %>% filter(element_name == "axis_title") %>% pull(family),
        size = .data %>% filter(element_name == "axis_title") %>% pull(size) / 1.5,
        color = .data %>% filter(element_name == "axis_title") %>% pull(color),
      ),
      axis.text = element_text(
        lineheight = .data %>% filter(element_name == "axis_text") %>% pull(lineheight),
        family = .data %>% filter(element_name == "axis_text") %>% pull(family),
        size = .data %>% filter(element_name == "axis_text") %>% pull(size) / 1.5,
        color = .data %>% filter(element_name == "axis_text") %>% pull(color),
      ),
      ...
    )
  }

  cli_alert_success("The theme() function has been created.")

  return(ad_hoc_theme_fun)
}
data(toy_raw_file_content)

library(ggplot2)

my_theme <- toy_raw_file_content %>% 
  extract_ggplot_theme() %>% 
  create_theme_fun()

#' \dontrun{
ggplot(data = iris) + 
  aes(x = Sepal.Width, fill = Species) + 
  geom_density() + 
  labs(title = "Sepal width of several species of iris",
       subtitle = "This plot respects the graphic design system defined in Figma",
       x = "Sepal width",
       y = "Density", 
       color = "Species") +
  my_theme()
#' }
test_that("create_theme_fun works", {

  data(toy_raw_file_content)

  my_theme <- toy_raw_file_content %>% 
    extract_ggplot_theme() %>% 
    create_theme_fun()

  expect_true(inherits(my_theme, "function"))

})
# Run but keep eval=FALSE to avoid infinite loop
# Execute in the console directly
fusen::inflate(flat_file = "dev/flat_ggplot_theme.Rmd", 
               vignette_name = "e - Create the {ggplot2} theme function",
               open_vignette = FALSE,
               check = FALSE,
               overwrite = TRUE)


ThinkR-open/swatch documentation built on April 7, 2022, 6:08 p.m.