Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/r-lib/gtable

The layout packages that powers ggplot2
https://github.com/r-lib/gtable

r

Last synced: 3 months ago
JSON representation

The layout packages that powers ggplot2

Awesome Lists containing this project

README

        

---
output: github_document
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
set.seed(42L)
options(width = 90)
```

# gtable

[![R-CMD-check](https://github.com/r-lib/gtable/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/r-lib/gtable/actions/workflows/R-CMD-check.yaml)
[![CRAN status](https://www.r-pkg.org/badges/version/gtable)](https://CRAN.R-project.org/package=gtable)
[![Codecov test coverage](https://codecov.io/gh/r-lib/gtable/branch/main/graph/badge.svg)](https://app.codecov.io/gh/r-lib/gtable?branch=main)
[![Lifecycle: stable](https://img.shields.io/badge/lifecycle-stable-brightgreen.svg)](https://lifecycle.r-lib.org/articles/stages.html#stable)

gtable is a layout engine built on top of the grid package. It is used to abstract away the creation of (potentially nested) grids of viewports into which graphic objects can be placed. gtable makes it easy to ensure alignment of graphic elements and piecemeal compositions of complex graphics. gtable is the layout engine powering [ggplot2](https://ggplot2.tidyverse.org) and is thus used extensively by many plotting functions in R without being called directly.

## Installation

You can install the released version of gtable from [CRAN](https://CRAN.R-project.org) with:

``` r
install.packages("gtable")
```

or use the remotes package to install the development version from [GitHub](https://github.com/r-lib/gtable)

```r
# install.packages("pak")
pak::pak("r-lib/gtable")
```

## Example
ggplot2 uses gtable for laying out plots, and it is possible to access the gtable representation of a plot for inspection and modification:

```{r, message=FALSE}
library(gtable)
library(ggplot2)

p <- ggplot(mtcars, aes(mpg, disp)) + geom_point()

p_table <- ggplotGrob(p)

p_table
```

A gtable object is a collection of graphic elements along with their placement in the grid and the dimensions of the grid itself. Graphic elements can span multiple rows and columns in the grid and be gtables themselves allowing for very complex automatically arranging layouts.

A gtable object is itself a grob, and can thus be drawn using standard functions from the grid package:

```{r}
library(grid)
grid.draw(p_table) # alternative use plot(p_table)
```

While most people will interact with gtable through ggplot2, it is possible to build a plot from the ground up.

```{r}
# Construct some graphical elements using grid
points <- pointsGrob(
x = runif(10),
y = runif(10),
size = unit(runif(10), 'cm')
)
xaxis <- xaxisGrob(at = c(0, 0.25, 0.5, 0.75, 1))
yaxis <- yaxisGrob(at = c(0, 0.25, 0.5, 0.75, 1))

# Setup the gtable layout
plot <- gtable(
widths = unit(c(1.5, 0, 1, 0.5), c('cm', 'cm', 'null', 'cm')),
heights = unit(c(0.5, 1, 0, 1), c('cm', 'null', 'cm', 'cm'))
)

# Add the grobs
plot <- gtable_add_grob(
plot,
grobs = list(points, xaxis, yaxis),
t = c(2, 3, 2),
l = c(3, 3, 2),
clip = 'off'
)

# Plot
grid.draw(plot)
```