add_panel

Share:

Description

A convenience function adding graphical objects to a gtable constructed by multi_panel_figure.

Usage

1
2
3
4
add_panel(figure, panel, top_panel = 1, bottom_panel = top_panel,
  left_panel = 1, right_panel = left_panel, label = NULL,
  label_just = c("right", "bottom"), panel_clip = c("on", "off", "inherit"),
  ...)

Arguments

figure

Object of classes multipanelfigure/ gtable as produced by multi_panel_figure and representing the figure the panel is to be placed in.

panel

Single character object representing path to a bitmap image (*.png, *.tiff/*.tif, *.jpg/*.jpeg), a ggplot object , a trellis.object, a gList object or a grob object to be placed in a multipanel figure. See 'Details'.

top_panel

Single numeric indicating the row index of the panel that is to be placed in the figure.

bottom_panel

Single numeric indicating the lower row index of the panel that is to be placed in the figure. Important for definition of panel spanning (see examples).

left_panel

Single numeric indicating the column index of the panel that is to be placed in the figure.

right_panel

Single numeric indicating the right column index of the panel that is to be placed in the figure. Important for definition of panel spanning (see examples).

label

Single character object defining the panel label used for automated annotation.

label_just

Justification for the label within the interpanel spacing grob to the top-left of the panel content grob. Passed to textGrob.

panel_clip

Should the display of panel contents be clipped at the panel borders? See viewport.

...

Additional arguments passed to download.file when adding PNG, TIFF, or JPEG panels from URL.

Details

Currently supported as panel-representing objects (panel) are

  1. ggplot2 ggplot objects.

  2. grid grob, gList, and gTree objects.

  3. lattice trellis.objects.

  4. Single character objects representing URLs or paths to readable portable network graphics (*.png), tagged image file format (*.tiff/*.tif), joint photographic experts group (*.jpg/*.jpeg) files, or support vector graphics (*.svg) which will be read and placed into panels as requested.

Note that *.jpg/*.jpeg files must be produced using the dimensions of the panel(s) they are to be placed in for sensible results. ggplot objects obviously auto-scale and *.tiff/*.tif, as well as *.png files have their native sizes read out of the file (which isn't working for *.jpg/*.jpeg). *.svg files are converted to a raster object the size of the panel. lattice-generated trellis.objects are converted tp grobs using grid.grabExpr(print(x)), the side effects of which with respect to plot formatting are not well studied.

Value

Returns the gtable object fed to it (figure) with the addition of the panel.

Author(s)

Johannes Graumann

See Also

gtable, multi_panel_figure, readTIFF, readPNG, readJPEG

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# Create the figure layout
(figure <- multi_panel_figure(
  widths = c(30,40,60),
  heights = c(40,60,60,60),
  panel_label_type = "upper-roman"))

# Fill the top-left panel using a grob object directly
a_grob <- grid::linesGrob(arrow = grid::arrow())
figure %<>% add_panel(a_grob)

# Add a ggplot object directly to the top row, second column.
a_ggplot <- ggplot2::ggplot(mtcars, ggplot2::aes(disp, mpg)) +
  ggplot2::geom_point()
figure %<>% add_panel(a_ggplot, left_panel = 2)

# JPEG, PNG, TIFF, and SVG images are added by passing the path to their file.
image_files <- system.file("extdata", package = "multipanelfigure") %>%
  dir(full.names = TRUE) %>%
  setNames(basename(.))

# Add the JPEG to the top row, third column
figure %<>% add_panel(image_files["rhino.jpg"], left_panel = 3)

# Add the PNG to the second and third row, first and second column
figure %<>% add_panel(
  image_files["Rlogo.png"],
  top_panel = 2, bottom_panel = 3, left_panel = 1, right_panel = 2)

# Add the TIFF to the second row, third column
figure %<>% add_panel(
  image_files["unicorn.svg"],
  top_panel = 2, left_panel = 3)

# lattice/trellis plot objects are also added directly
Depth <- lattice::equal.count(quakes$depth, number=4, overlap=0.1)
a_lattice_plot <- lattice::xyplot(lat ~ long | Depth, data = quakes)
# Add the lattice plot to the third row, third column
figure %<>% add_panel(
  a_lattice_plot,
  top_panel = 3, left_panel = 3)

# Incorporate a gList object (such as produced by VennDigram)
if(requireNamespace("VennDiagram"))
{
  a_venn_plot <- VennDiagram::draw.pairwise.venn(50, 30, 20, ind = FALSE)
  # Add the Venn diagram to the fourth row, first and second columns
  (figure %<>% add_panel(
    a_venn_plot,
    top_panel = 4, left_panel = 1, right_panel = 2))
}

# Incorporate a base plot figure
a_base_plot <- capture_base_plot(
 heatmap(
   cor(USJudgeRatings), Rowv = FALSE, symm = TRUE, col = topo.colors(16),
   distfun = function(c) as.dist(1 - c), keep.dendro = TRUE,
   cexRow = 0.5, cexCol = 0.5))
# Add the heatmap to the fourth row, third column
(figure %<>% add_panel(
  a_base_plot,
  top_panel = 4, left_panel = 3))