plotgardener uses a coordinate-based plotting system to define the size and location of plots. This system makes the plotting process intuitive and absolute, meaning that plots cannot be squished and stretched based on their relative sizes. This also allows for precise control of the size of each visualization and the location of all plots, annotations, and text.

All plotgardener page functions begin with the page prefix.

Users can create a page in their preferred size and unit of measurement using pageCreate(). Within this function the user can also set gridlines in the vertical and horizontal directions with xgrid and ygrid, respectively. By default these values are set to 0.5 of the unit. In the following example we demonstrate creating a standard 8.5 x 11 inch page:

pageCreate(width = 8.5, height = 11, default.units = "inches")

Or we could create a smaller sized page in a different set of units with different gridlines:

pageCreate(width = 8, height = 8, xgrid = 1, ygrid = 1, default.units = "cm")

We could turn off gridlines entirely by setting xgrid and ygrid to 0:

pageCreate(
width = 3, height = 3, xgrid = 0, ygrid = 0,
default.units = "inches"
)

If we want more specific gridlines on our page, we can use the pageGuideHorizontal() and pageGuideVertical() functions:

pageCreate(width = 3, height = 3, default.units = "inches")
## Add a horizontal guide at y = 2.25 inches
pageGuideHorizontal(y = 2.25, default.units = "inches")
## Add a vertical guide at x = 0.75 inches
pageGuideVertical(x = 0.75, default.units = "inches")

We can also remove all guidelines from the plot once we are finished using guides by using the pageGuideHide() function:

## Create page
pageCreate(width = 3, height = 3, default.units = "inches")

## Remove guides
pageGuideHide()

### Coordinate systems and units

plotgardener is compatible with numerous coordinate systems, which are flexible enough to be used in combination. Brief descriptions of the most commonly used plotgardener coordinate systems are as follows:

Coordinate System Description
“npc” Normalized Parent Coordinates. Treats the bottom-left corner of the plotting region as the location (0,0) and the top-right corner as (1,1).
“snpc” Squared Normalized Parent Coordiantes. Placements and sizes are expressed as a proportion of the smaller of the width and height of the plotting region.
“native” Placements and sizes are relative to the x- and y-scales of the plotting region.
“inches” Placements and sizes are in terms of physical inches.
“cm” Placements and sizes are in terms of physical centimeters.
“mm” Placements and sizes are in terms of physical millimeters.
“points” Placements and sizes are in terms of physical points. There are 72.27 points per inch.

We can set the page in one coordinate system, but then place and arrange our plots using other coordinate systems. For example, we can set our page as 3 x 3 inches:

pageCreate(width = 3, height = 3, default.units = "inches")

But we can then switch to npc coordinates to plot something in the center of the page at (0.5, 0.5) npc. The unit() function allows us to easily specify x, y, width, and height in combinations of different units in one plotting function call.

plotRect(
x = unit(0.5, "npc"), y = unit(0.5, "npc"), width = 1, height = 1,
default.units = "inches"
)

The native coordinate system is particularly useful for annotation functions that can plot relative to the genomic scales of a plot. For example, we can use annoText() to annotate some text at a specific genomic location in a plot:

pageCreate(
width = 5, height = 1.5, default.units = "inches",
showGuides = FALSE, xgrid = 0, ygrid = 0
)

library(plotgardenerData)
data("IMR90_ChIP_H3K27ac_signal")
signalPlot <- plotSignal(
data = IMR90_ChIP_H3K27ac_signal,
chrom = "chr21", chromstart = 28000000, chromend = 30300000,
assembly = "hg19",
x = 0.5, y = 0.25, width = 4, height = 0.75, default.units = "inches"
)
annoGenomeLabel(plot = signalPlot, x = 0.5, y = 1.01)

## Annotate text at average x-coordinate of data peak
peakScore <- IMR90_ChIP_H3K27ac_signal[which(
IMR90_ChIP_H3K27ac_signal$score == max(IMR90_ChIP_H3K27ac_signal$score)
), ]
peakPos <- round((min(peakScore$start) + max(peakScore$end)) * 0.5)
annoText(
plot = signalPlot, label = format(peakPos, big.mark = ","), fontsize = 8,
x = unit(peakPos, "native"), y = unit(1, "npc"),
just = "bottom"
)

### Working with plot objects

In plotgardener all plot objects are boxes, with user-defined positions and sizes. All plot objects can be placed on a page using the placement arguments (e.g. x, y, width, height, just, default.units, …). The page sets the origin of the plot at the top left corner of the page. By default, the x and y arguments place the top-left corner of a plot in the specified position on the page while the width and height arguments define the size of the plot.

For example, if users want the top-left corner of their plot to be 0.5 inches down from the top of the page and 0.5 inches from the left…

and the plot to be 2 inches wide and 1 inch tall…

plotgardener can make the plot with these exact dimensions:

## Create page
pageCreate(width = 3, height = 3, default.units = "inches")

## Plot rectangle
plotRect(
x = 0.5, y = 0.5, width = 2, height = 1,
just = c("left", "top"), default.units = "inches"
)

plotgardener also provides the helper function pagePlotPlace() for placing plot objects that have been previously defined:

## Load data
library(plotgardenerData)
data("IMR90_ChIP_H3K27ac_signal")

## Create page
pageCreate(width = 3, height = 3, default.units = "inches")

## Define signal plot
signalPlot <- plotSignal(
data = IMR90_ChIP_H3K27ac_signal,
chrom = "chr21", chromstart = 28000000, chromend = 30300000,
assembly = "hg19",
draw = FALSE
)

## Place plot on page
pagePlotPlace(
plot = signalPlot,
x = 0.5, y = 0.5, width = 2, height = 1,
just = c("left", "top"), default.units = "inches"
)

and pagePlotRemove() for removing plots from a page:

# Load data
library(plotgardenerData)
data("IMR90_ChIP_H3K27ac_signal")

## Create page
pageCreate(width = 3, height = 3, default.units = "inches")

## Plot and place signal plot
signalPlot <- plotSignal(
data = IMR90_ChIP_H3K27ac_signal,
chrom = "chr21", chromstart = 28000000, chromend = 30300000,
assembly = "hg19",
x = 0.5, y = 0.5, width = 2, height = 1,
just = c("left", "top"), default.units = "inches"
)

## Remove signal plot
pagePlotRemove(plot = signalPlot)

These functions give the users additional flexibility in how they create their R scripts and plotgardener layouts.

### Using the just parameter

While the x, y, width, and height parameters are relative to the top-left corner of the plot by default, the just parameter provides additional flexibility by allowing users to change the placement reference point. The just parameter accepts a character or numeric vector of length 2 describing the horizontal and vertical justification (or reference point), respectively.

The just parameter can be set using character strings "left", "right", "center", "bottom" and "top":

Or it can be set using numeric values where 0 means left/bottom, 1 means right/top, and 0.5 means center:

This is particularly useful when an object needs to be aligned in reference to another plot object or page marker. For example, in the Hi-C plot below we might want to align the top-right corner of the heatmap legend to the 3-inch mark. There is no need to calculate the top-left position (i.e. 3 inches - (legend width)) to determine where to place the heatmap legend. Instead we can change the just parameter to just=c('right', 'top'):

## Load example Hi-C data
library(plotgardenerData)
data("IMR90_HiC_10kb")

## Create a plotgardener page
pageCreate(width = 3.25, height = 3.25, default.units = "inches")

## Plot Hi-C data with placing information
hicPlot <- plotHicSquare(
data = IMR90_HiC_10kb,
chrom = "chr21", chromstart = 28000000, chromend = 30300000,
assembly = "hg19",
x = 0.25, y = 0.25, width = 2.5, height = 2.5, default.units = "inches"
)

## Add color scale annotation with just = c("right", "top")
annoHeatmapLegend(
plot = hicPlot,
x = 3, y = 0.25, width = 0.1, height = 1.25,
just = c("right", "top"), default.units = "inches"
)

## Session Info

sessionInfo()
#> R version 4.2.0 (2022-04-22)
#> Platform: x86_64-apple-darwin17.0 (64-bit)
#> Running under: macOS Big Sur/Monterey 10.16
#>
#> Matrix products: default
#> BLAS:   /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRblas.0.dylib
#> LAPACK: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRlapack.dylib
#>
#> locale:
#> [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
#>
#> attached base packages:
#> [1] grid      stats     graphics  grDevices utils     datasets  methods
#> [8] base
#>
#> other attached packages:
#> [1] plotgardenerData_1.1.0 plotgardener_1.2.0
#>
#> loaded via a namespace (and not attached):
#>  [1] MatrixGenerics_1.8.0        Biobase_2.56.0
#>  [3] sass_0.4.1                  jsonlite_1.8.0
#>  [5] bslib_0.3.1                 assertthat_0.2.1
#>  [7] highr_0.9                   stats4_4.2.0
#>  [9] yulab.utils_0.0.4           GenomeInfoDbData_1.2.8
#> [11] Rsamtools_2.12.0            yaml_2.3.5
#> [13] pillar_1.7.0                lattice_0.20-45
#> [15] glue_1.6.2                  digest_0.6.29
#> [17] GenomicRanges_1.48.0        RColorBrewer_1.1-3
#> [19] XVector_0.36.0              colorspace_2.0-3
#> [21] htmltools_0.5.2             Matrix_1.4-1
#> [23] strawr_0.0.9                XML_3.99-0.9
#> [25] pkgconfig_2.0.3             zlibbioc_1.42.0
#> [27] purrr_0.3.4                 scales_1.2.0
#> [29] ggplotify_0.1.0             BiocParallel_1.30.0
#> [31] tibble_3.1.6                generics_0.1.2
#> [33] IRanges_2.30.0              ggplot2_3.3.5
#> [35] ellipsis_0.3.2              cachem_1.0.6
#> [37] SummarizedExperiment_1.26.0 BiocGenerics_0.42.0
#> [39] cli_3.3.0                   magrittr_2.0.3
#> [41] crayon_1.5.1                memoise_2.0.1
#> [43] evaluate_0.15               fs_1.5.2
#> [45] fansi_1.0.3                 textshaping_0.3.6
#> [47] tools_4.2.0                 data.table_1.14.2
#> [49] BiocIO_1.6.0                lifecycle_1.0.1
#> [51] matrixStats_0.62.0          stringr_1.4.0
#> [53] plyranges_1.16.0            S4Vectors_0.34.0
#> [55] munsell_0.5.0               DelayedArray_0.22.0
#> [57] Biostrings_2.64.0           compiler_4.2.0
#> [59] pkgdown_2.0.3               jquerylib_0.1.4
#> [61] GenomeInfoDb_1.32.0         gridGraphics_0.5-1
#> [63] systemfonts_1.0.4           rlang_1.0.2
#> [65] RCurl_1.98-1.6              rstudioapi_0.13
#> [67] rjson_0.2.21                bitops_1.0-7
#> [69] rmarkdown_2.14              restfulr_0.0.13
#> [71] gtable_0.3.0                DBI_1.1.2
#> [73] curl_4.3.2                  R6_2.5.1
#> [75] GenomicAlignments_1.32.0    knitr_1.39
#> [77] dplyr_1.0.9                 rtracklayer_1.56.0
#> [79] fastmap_1.1.0               utf8_1.2.2
#> [81] rprojroot_2.0.3             ragg_1.2.2
#> [83] desc_1.4.1                  stringi_1.7.6
#> [85] parallel_4.2.0              Rcpp_1.0.8.3
#> [87] vctrs_0.4.1                 tidyselect_1.1.2
#> [89] xfun_0.30