Skip to main content

Plotgardener in R

Visualizing Genomic Data with plotgardener

plotgardener is an R package for creating publication-quality, coordinate-based multi-panel genomic figures. It provides precise control over plot placement and arrangement.

You can download RStudio to work on R scripts. For plotgardener, you have to have bigwig files local to the computer, rather than having them as public URLs and make sure you know the genome assembly that the bigwig files are aligned to.

Their documentation is very good and has a lot of examples, so I recommend following along with the vignettes on their website: Plotgardener pages

Why plotgardener?

  • Coordinate-based plotting - Place plots at exact positions on the page
  • Multi-panel figures - Combine multiple genomic tracks in one figure
  • Publication quality - Generate high-resolution figures ready for publication
  • Flexible - Supports various genomic data formats (BAM, bigWig, bedpe, BED, etc.)
  • Customizable - Fine control over every visual element

Installation

# Install from Bioconductor
if (!require("BiocManager", quietly = TRUE))
    install.packages("BiocManager")

BiocManager::install("plotgardener")

Basic Workflow

1. Load Libraries and Data

library("plotgardener")
library("org.Mm.eg.db")
library("TxDb.Mmusculus.UCSC.mm9.knownGene")
library("AnnotationHub")
library("RColorBrewer")
library("grid")

# Load your genomic data (e.g., bigWig files)
ATAC_bwRegion <- "path/to/ATAC.bigWig"

2. Create a Page

Start by defining a page with specific dimensions:

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

3. Define Parameters

Set up genomic coordinates and common parameters:

  ## Set genomic and dimension parameters in a `params` object
  params_c <- pgParams(chrom = "chr11", chromstart = 31880000, chromend = 32432000, 
                       assembly = "mm9", x = 0, width = 7, default.units = "inches")
)

4. Add Genomic Tracks

Add various types of genomic data:

# Add a gene track
  genes_gm <- plotGenes(params = params_c, stroke = 0.05, fontsize = 8,
                        geneHighlights = data.frame("gene" = c("Hba-a1","Hba-a2"),"color" = c("red", "red")),
                        geneBackground = "#081D58", strandLabels = TRUE, 
                        y = 1, height = 0.5)

# Add a signal track (bigWig)
  ATAC <- plotSignal(data = ATAC_bwRegion, params = params_c,
                     fill = "#262626", linecolor = "#262626", y=1.1, height = 1,
                     scale =TRUE, fontsize = 8)

5. Add Annotations

Include labels, legends, and scales:

# Add genome label
annoGenomeLabel(plot = genes_gm, params = params_c, scale = "bp", fontsize = 10, y = 0.75)

# Add Labels
plotText(label = "ATAC", fontcolor = "#262626", fontsize =10,fontface = "bold",
        x = 0.5, y = 1.5, just = c("left","center"), default.units = "inches")

# You can add annotations to highlight specific regions, for example:
annoHighlight(plot = ATAC, params =  params_c,chrom = "chr11", chromstart = 32145500, chromend = 32146000,
            y = 1.1, height = 1, just = c("left", "top"), default.units = "inches",fill= "grey")

BED files

# Plot peaks
plotRanges(data = "peaks.bed", params = params_c,
    x = 0.5, y = 2, height = 0.5, fill = "red")

Saving Figures

# Save as PDF
pdf("figure.pdf", width = 7.5, height = 6)
# ... your plotting code ...
dev.off()

# Save as PNG (high resolution)
png("figure.png", width = 7.5, height = 6, units = "in", res = 300)
# ... your plotting code ...
dev.off()

Example: Putting it all together and also with a Hi-C track

## Load plotgardener
library("plotgardener")
library("org.Mm.eg.db")
library("TxDb.Mmusculus.UCSC.mm9.knownGene")
library("AnnotationHub")
library("RColorBrewer")
library("grid")

## Set working directory
setwd("Path/to/your/working/directory")
getwd()

##### HIC matrix #####
### These HiC matrix files are generated from HiC-Pro and then processed with ICE normalization. 

# This is the HiC matrix file
Iced.matrix= read.delim("Data/WT_Erythroid_Rep1_ontarget_2000_iced.matrix", sep = "\t", 
                        header=TRUE, col.names = c("Bin1", "Bin2", "Interaction_freq"), skip=0)
# This is the bin index file that corresponds to the HiC matrix file, which has the genomic coordinates of each bin in the matrix.
bin_index= read.delim("Data/WT_Erythroid_Rep1_ontarget_2000_abs.bed", sep = "\t", 
                      header=TRUE, col.names = c("Chr", "Start", "End", "Bin"), skip=0)

### All the following is to convert the format to be read
bin_index = bin_index[bin_index$Chr == 'chr11',]
bin_index = bin_index[bin_index$Start >= 29898000 & bin_index$End <= 33224000,]
bin_index_short = cbind.data.frame(bin_index$Start,bin_index$Bin)
colnames(bin_index_short) = c("Start","Bin")
colnames(Iced.matrix) = c("Bin", "Bin2", "Interaction_freq")

data_1 = na.omit(merge(Iced.matrix,bin_index_short, by = "Bin", all.x=TRUE, all.y=TRUE))
colnames(data_1) = c("Bin1", "Bin","Interaction_freq", "Start1")
data_2 = na.omit(merge(data_1,bin_index_short, by = "Bin", all.x=TRUE, all.y=TRUE))
colnames(data_2) = c("Bin2", "Bin1", "Interaction_freq","Start1","Start2")

HiC_data = na.omit(cbind.data.frame(data_2$Start1,data_2$Start2,data_2$Interaction_freq))
colnames(HiC_data) = c("chr11", "chr11.1", "Interaction_freq")

## Load bigwig files for the same region
DHS_bwRegion = readBigwig(file="Bigwigs/All_Good_Combined_C57_DHS_Combined.bw", 
                         chrom = "chr11", chromstart =32000000, chromend =32365000)

ATAC_bwRegion = readBigwig(file="Bigwigs/C57BL6_Spleen_ATAC_RPKM_Average.bw", 
                               chrom = "chr11", chromstart =32000000, chromend =32365000)

CTCF_bwRegion = readBigwig(file="Bigwigs/C57_CTCF_1_RPKM.bw", 
                                  chrom = "chr11", chromstart =32000000, chromend =32365000)

# Open a PDF device to save the figure
pdf("mm9_globin_tiledC.pdf", width=7.5, height=7)
{
    # Create a page
    pageCreate(width = 6, height = 7, default.units = "inches")
  
     ## Set genomic and dimension parameters in a `params` object
    params_c <- pgParams(chrom = "chr11", chromstart = 32020000, chromend = 32270000, 
                       assembly = "mm9", x = 0, width = 6, default.units = "inches")
  
    hicPlot = plotHicTriangle(params = params_c, data = HiC_data,  resolution = 2000, zrange = c(0, 400),
                            palette = colorRampPalette(brewer.pal(n = 9, "Blues")),
                            x = 0, y = 0, width = 6, height = 3, just =c("left", "top"))
  
    annoHeatmapLegend(plot = hicPlot, x = 5, y = 0, width = 0.13, height = 1.2, just = c("right", "top"))
  
    ## Plot signals
    DHS <- plotSignal(data = DHS_bwRegion, params = params_c,
                    fill = "black", linecolor = "black", y=3, height = 0.75, width = 6, 
                    scale =FALSE, fontsize = 8)
  
    ATAC <- plotSignal(data = ATAC_bwRegion, params = params_c,
                     fill = "darkgrey", linecolor = "darkgrey", y=3.75, height = 0.75, width = 6, 
                     scale =FALSE, fontsize = 8)
  
    CTCF <- plotSignal(data = CTCF_bwRegion, params = params_c,
                     fill = "navy", linecolor = "navy", y=4.5, height = 0.75, width = 6, 
                     scale =FALSE, fontsize = 8)

    #Labels
    plotText(label = "DNaseHS", fontcolor = "black", fontsize =10,fontface = "bold",
           x = 0, y = 3.25, just = c("left","center"), default.units = "inches")
    plotText(label = "ATAC", fontcolor = "darkgrey", fontsize =10,fontface = "bold",
           x = 0, y = 4, just = c("left","center"), default.units = "inches")
    plotText(label = "CTCF", fontcolor = "navy", fontsize =10,fontface = "bold",
           x = 0, y = 4.75, just = c("left","center"), default.units = "inches")
  
    ## Plot genes
    genes_gm <- plotGenes(params = params_c, stroke = 0.1, fontsize = 10,
                        geneHighlights = data.frame("gene" = c("Hba-a1","Hba-a2"),"color" = c("red", "red")),
                        geneBackground = "#081D58", strandLabels = TRUE, 
                        y = 5.7, x=0, height = 0.5)
  
    ## Annotate genome label
    annoGenomeLabel(plot = genes_gm, params = params_c, scale = "bp", fontsize = 10, y = 6.3)

    ## WT position Enhancers 
    #R1
    annoHighlight(plot = DHS, params =  params_c,chrom = "chr11", chromstart = 32145500, chromend = 32146000,
                y = 3, height = 2.25, just = c("left", "top"), default.units = "inches",fill= "#C7E9B4")
    #R2
    annoHighlight(plot = DHS, params =  params_c,chrom = "chr11", chromstart = 32150900, chromend = 32151400,
                y = 3, height = 2.25, just = c("left", "top"), default.units = "inches", fill= "#C7E9B4")
    #R3
    annoHighlight(plot = DHS, params =  params_c,chrom = "chr11", chromstart = 32156100, chromend = 32156600,
                y = 3, height = 2.25, just = c("left", "top"), default.units = "inches", fill= "#C7E9B4")
    #Rm 
    annoHighlight(plot = DHS, params =  params_c,chrom = "chr11", chromstart = 32165100, chromend = 32165600,
                y = 3, height = 2.25, just = c("left", "top"), default.units = "inches", fill= "#C7E9B4")
    #R4
    annoHighlight(plot = DHS, params =  params_c,chrom = "chr11", chromstart = 32168900, chromend = 32169400,
                y = 3, height = 2.25, just = c("left", "top"), default.units = "inches",fill= "#C7E9B4")
    #Remove guide
    pageGuideHide()
}
# Close the PDF device
dev.off()

Tips and Best Practices

  1. Units: Use consistent units throughout (typically “inches” for publications)
  2. Alignment: Use the same x position and width for vertically stacked plots
  3. Colors: Choose color schemes appropriate for your data type
  4. Resolution: For Hi-C data, match resolution to your region size
  5. Save output: Use pdf() or png() to save high-quality figures

Additional Resources