Learn R Programming

gridify (version 0.7.4)

complex_layout: Complex Layout for a gridify object

Description

This function creates a complex layout for a gridify object. The layout consists of six rows and three columns for headers, titles, notes and footnotes around the output.

Usage

complex_layout(
  margin = grid::unit(c(t = 0.1, r = 0.1, b = 0.1, l = 0.1), units = "npc"),
  global_gpar = grid::gpar(),
  background = grid::get.gpar()$fill,
  scales = c("fixed", "free")
)

Value

A gridifyLayout object.

Arguments

margin

A unit object specifying the margins around the output. Default is 10% of the output area on all sides.

global_gpar

A gpar object specifying the global graphical parameters. Must be the result of a call to grid::gpar().

background

A string specifying the background fill colour. Default grid::get.gpar()$fill for a white background.

scales

A string, either "free" or "fixed". By default, "fixed" ensures that text elements (titles, footers, etc.) retain a static height, preventing text overlap while maintaining a structured layout. However, this may result in different height proportions between the text elements and the output.

The "free" option makes the row heights proportional, allowing them to scale dynamically based on the overall output size. This ensures that the text elements and the output maintain relative proportions.

Details

The layout consists of six rows for headers, titles, object (figure or table), notes, and footnotes. The object is placed in the fourth row.

  • With "free" scales, the row heights are 5%, 5%, 5%, 70%, 5%, and 10% of the area, respectively.

  • With "fixed" scales, the row heights are adjusted by the number of lines for all text elements around the object, with the remaining area occupied by the object. Note that reducing the output space will retain the space for all text elements, making the object appear smaller.

Examples

Run this code
complex_layout()

# (to use |> version 4.1.0 of R is required, for lower versions we recommend %>% from magrittr)
library(magrittr)

gridify(
  object = ggplot2::ggplot(data = mtcars, ggplot2::aes(x = mpg, y = wt)) +
    ggplot2::geom_line(),
  layout = complex_layout()
) %>%
  set_cell("header_left", "Left Header") %>%
  set_cell("header_middle", "Middle Header") %>%
  set_cell("header_right", "Right Header") %>%
  set_cell("title", "Title") %>%
  set_cell("subtitle", "Subtitle") %>%
  set_cell("note", "Note") %>%
  set_cell("footer_left", "Left Footer") %>%
  set_cell("footer_middle", "Middle Footer") %>%
  set_cell("footer_right", "Right Footer")

gridify(
  object = ggplot2::ggplot(data = mtcars, ggplot2::aes(x = mpg, y = wt)) +
    ggplot2::geom_line(),
  layout = complex_layout(margin = grid::unit(c(t = 0.2, r = 0.2, b = 0.2, l = 0.2), units = "npc"))
) %>%
  set_cell("header_left", "Left Header") %>%
  set_cell("header_right", "Right Header") %>%
  set_cell("title", "Title") %>%
  set_cell("note", "Note") %>%
  set_cell("footer_left", "Left Footer")

gridify(
  object = gt::gt(head(mtcars)),
  layout = complex_layout(
    margin = grid::unit(c(t = 0.2, r = 0.2, b = 0.2, l = 0.2), units = "npc"),
    global_gpar = grid::gpar(col = "blue", fontsize = 18)
  )
) %>%
  set_cell("header_left", "Left Header") %>%
  set_cell("header_right", "Right Header") %>%
  set_cell("title", "Title") %>%
  set_cell("note", "Note") %>%
  set_cell("footer_left", "Left Footer")

Run the code above in your browser using DataLab