ggfoundry

R
package
A new package casting fillable shapes for ‘ggplot2’
Author

Carl Goodwin

Published

June 17, 2024

A hexagon stands next to a wall holding a newly cast shape. Along the wall a series of recently-cast shapes are cooling. And the hex itself shows a shape being cast from molton metal.

There are various strategies for plotting many shapes in R. But it’s not always possible to find what you want, nor manipulate it in the way you would like. ggfoundry offers arbitrary colourable and fillable shapes for ggplot2. The motivation behind it, how it contrasts with existing strategies, a showcase of example uses and a list of available shapes, are all covered on the package website.

This short post merely says: “Hey, I’m new in town and reachable on CRAN”. And to offer a different perspective, here’s the method used to cast these new fillable shapes.

The Method

A flow diagram showing the method used to create and plot new shapes.

Workflow Steps

Outside R

The foundry process begins with a drawing made by hand in Adobe Fresco. In a similar vein to the way ggplot2 builds up geom layers, the drawing consists of layers: An outline and a fill. These layers are exported to Adobe Capture for the creation of an SVG pair.

Not Shipped with the R Package

The R packages book recommends preserving the origin story of package data. So the R script for the next step in the process is saved in the package’s data-raw folder. It only needs to be run when a new shape is being cast or altered.

The grImport2 package requires an SVG image that has been generated by the Cairo graphics library. So, convertPicture() handles this intermediate step. And readPicture() creates the object of class “Picture”.

usethis::use_data() saves a list of the Picture objects in sysdata.rda using the optimal compression method (“xz” works best for these data). This is how the shapes are shipped to the R user.

Shipped with the R Package

The Picture list is made available when library(ggfoundry) is called. To add these new fillable shapes to a ggplot, the user adds a geom_casting() layer specifying the required shape as a string, e.g. “box”, and optionally a fill and colour.

geom_casting() finds the selected shape in the Picture list and calls symbolsGrob() once per group to enable the drawing of it at several locations. In other words, each grob (grid graphical object) has the x and y coordinates for all data frame rows in that group along with the associated shape. Fill and colour are also applied by group.