Package 'figpatch'

Title: Easily Arrange External Figures with Patchwork Alongside 'ggplot2' Figures
Description: For including external figures into an assembled {patchwork}. This enables the creation of more complex figures that include images alongside plots.
Authors: Brady Johnston [aut, cre]
Maintainer: Brady Johnston <[email protected]>
License: MIT + file LICENSE
Version: 0.2
Built: 2024-11-03 03:35:22 UTC
Source: https://github.com/BradyAJohnston/figpatch

Help Index


Parse Image to a Fig

Description

Read image and convert to ggplot object, for use with other ggplot objects when assembling with the {patchwork} package. Can also specify a border.

Usage

fig(
  path,
  aspect.ratio = "default",
  link_dim = TRUE,
  b_col = NULL,
  b_size = 1,
  b_pos = "offset",
  b_margin = ggplot2::margin(4, 4, 4, 4)
)

Arguments

path

Path to image file.

aspect.ratio

Manually override the image's aspect ratio or set "free" to allow fig to be resized by patchwork.

link_dim

Logical, whether to lock the dimensions & aspect.ratio of the aligned plots to that of this fig.

b_col

Colour of the border line.

b_size

Size of the border line.

b_pos

Whether the border is 'offset' (expands out from figure) or 'inset' and expands inwards, partially covering up the figure.

b_margin

Margin around the fig. Use ggplot2::margin()

Value

{ggplot2} object

Examples

library(figpatch)
library(ggplot2)

# Attach the fig image file
image <- system.file("extdata", "fig.png", package = "figpatch", mustWork = TRUE)

# Read in the image as a 'fig'
img <- fig(image)

img

Add Label to a Fig

Description

Add Label to a Fig

Usage

fig_lab(
  fig,
  lab,
  pos = "bottom",
  fontfamily = NULL,
  fontface = NULL,
  colour = NULL,
  size = NULL,
  lineheight = NULL,
  hjust = NULL,
  vjust = NULL,
  angle = NULL,
  margin = ggplot2::margin(4, 4, 4, 4)
)

Arguments

fig

Fig to be labelled.

lab

String of label to be added to the fig.

pos

Position of the fig, either 'top', 'bottom', 'left', or 'right'.

fontfamily

Font family for the label.

fontface

Font face for the label (i.e. "italic")

colour

Colour of the label text.

size

Size of the label text.

lineheight

Lineheight of the label text.

hjust

hjust of the label text.

vjust

vjust of the label text.

angle

Angle of the label text.

margin

Margin around the label text. Use ggplot2::margin()

Value

{ggplot2} object

Examples

library(figpatch)
library(ggplot2)

# Attach the fig image file
image <- system.file("extdata", "fig.png", package = "figpatch", mustWork = TRUE)

# Read in the image as a 'fig'
img <- fig(image)

# add the fig label
fig_lab(
  img,
  lab = "Below you will find a fig!",
  pos = "top",
  size = 20
)

Scales the Dimensions of Multiple Figs

Description

Finds the dimensions of the largest figs, and adds a border of the given colour around the other figs, to ensure they are all of the same dimensions and scale properly when displayed on in a patchwork together.

Usage

fig_scale(..., border_colour = "transparent")

Arguments

...

Multiple figs created with fig().

border_colour

Colour of the border to be added around the smaller figs.

Value

A list of figs which have been resized, which can be input directly into fig_wrap() or patchwork::wrap_plots().

Examples

library(figpatch)
fl <- image_path <- system.file("extdata",
  package = "figpatch",
  mustWork = TRUE
) %>%
  list.files(
    pattern = "png",
    full.names = TRUE
  )

# without scaling
fl %>%
  lapply(fig) %>%
  fig_wrap(ncol = 2)

# with scaling
fl %>%
  lapply(fig) %>%
  fig_scale() %>%
  fig_wrap(ncol = 2)

Add tags and a caption to a fig.

Description

Add tags and a caption to a fig.

Usage

fig_tag(
  plot,
  tag,
  pos = "topleft",
  x_nudge = 0,
  y_nudge = 0,
  colour = NULL,
  alpha = NULL,
  hjust = NULL,
  vjust = NULL,
  fontsize = 12,
  fontface = NULL,
  fontfamily = NULL
)

Arguments

plot

Plot from img2plot function.

tag

Label to add to plot.

pos

Position of label (Default 'topleft').

x_nudge

Minor adjustments to the x position in relative plot coordindates (0 being furthest left, 1 being furthest right).

y_nudge

Minor adjustments to the y position in relative plot coordinates (0 being the bottom, 1 being the top).

colour

Colour of label text.

alpha

Alpha of label text.

hjust

hjust of plot label.

vjust

vjust of plot label.

fontsize

Fontsize of label (in points).

fontface

The font face (bolt, italic, ...)

fontfamily

Fontfamily of plot label.

Value

{ggplot2} object

Examples

library(figpatch)
library(ggplot2)

# Attach the fig image file
image <- system.file("extdata", "fig.png",
  package = "figpatch", mustWork =
    TRUE
)

# Read in the image as a 'fig'
img <- fig(image)

# Add tags on top of the figs
img1 <- fig_tag(img, "A")
img2 <- fig_tag(img, "B", pos = "topright")

# assemble the patchwork
patchwork::wrap_plots(img1, img2)

Quickly arrange and label multiple figs.

Description

Quickly arrange and label multiple figs.

Usage

fig_wrap(
  figs,
  tag = NULL,
  prefix = NULL,
  suffix = NULL,
  pos = "topleft",
  x_nudge = 0,
  y_nudge = 0,
  nrow = NULL,
  ncol = NULL,
  colour = NULL,
  alpha = NULL,
  hjust = NULL,
  vjust = NULL,
  fontsize = NULL,
  fontfamily = NULL,
  fontface = NULL,
  b_col = NULL,
  b_size = 1,
  b_pos = "offset",
  b_margin = ggplot2::margin(4, 4, 4, 4)
)

Arguments

figs

List of figs from fig().

tag

Tags to be applied to figs. Begins with first uppercase or lowercase letter supplied, or number, and continues the sequence. "A" labels them 'A', 'B', etc. "c" labels them 'c', 'd', 'e' etc.

prefix

Prefix for each tag

suffix

Suffix for each tag

pos

Position for label, to be passed to fig_tag().

x_nudge

Minor adjustments to the x position in relative plot coordindates (0 being furthest left, 1 being furthest right).

y_nudge

Minor adjustments to the y position in relative plot coordinates (0 being the bottom, 1 being the top).

nrow

Number of rows in final patchwork.

ncol

Number of cols in final patchwork.

colour

Colour for each tag

alpha

Alpha for each tag

hjust

hjust for each tag

vjust

vjust for each tag

fontsize

Fontsize for each tag

fontfamily

Fontfamily for each tag

fontface

Fontface for each tag

b_col

Colour of individual fig borders.

b_size

Size of individual fig borders (in mm).

b_pos

Either "offset" and expanding outwards from borders of figure, or "inset" and expanding inwards and partially covering the figure.

b_margin

Margins to adjust around the figs. Use ggplot2::margin()

Value

patchwork patch of supplied figs.

Examples

library(figpatch)
library(ggplot2)

# Attach the fig image file
image <- system.file("extdata", "fig.png", package = "figpatch", mustWork = TRUE)

# Read in the image as a 'fig'
img <- fig(image)

# multiple figs
figs <- lapply(1:9, function(x) img)

# wrap the figs
fig_wrap(figs)

# Wrap the figs and auto-tag
fig_wrap(figs, tag = "A", suffix = ")")

# Wrap figs, auto-tag and adds border.
fig_wrap(figs, tag = 1, prefix = "(", suffix = ")", b_col = "black")