Last data update: 2014.03.03

R: ggplot methods
ggplotR Documentation

ggplot methods

Description

These methods extend ggplot to support several types of Bioconductor objects, as well as some base types like matrix. They return a ggbio object, which stores the original data object. Please check the corresponding method for mold to see how an object is coerced into a data.frame.

Usage

## S3 method for class 'Vector'
ggplot(data, mapping = aes(), ...,
                        environment = parent.frame())
## S3 method for class 'Seqinfo'
ggplot(data, mapping = aes(), ...,
                        environment = parent.frame())
## S3 method for class 'ExpressionSet'
ggplot(data, mapping = aes(), ...,
                        environment = parent.frame())
## S3 method for class 'RsamtoolsFile'
ggplot(data, mapping = aes(), ...,
                        environment = parent.frame())
## S3 method for class 'TxDbOREnsDb'
ggplot(data, mapping = aes(), ...,
                        environment = parent.frame())
## S3 method for class 'BSgenome'
ggplot(data, mapping = aes(), ...,
                        environment = parent.frame())
## S3 method for class 'matrix'
ggplot(data, mapping = aes(), ...,
                        environment = parent.frame())
## S3 method for class 'character'
ggplot(data, mapping = aes(), ...,
                        environment = parent.frame())
## S3 method for class 'SummarizedExperiment'
ggplot(data, mapping = aes(),
                        assay.id = 1L, ..., environment = parent.frame())
## S3 method for class 'GAlignments'
ggplot(data, mapping = aes(), ...,
                        environment = parent.frame())
## S3 method for class 'VCF'
ggplot(data, mapping = aes(), ...,
                        environment = parent.frame())

Arguments

data

original data object.

mapping

the aesthetic mapping.

...

other arguments passed to specific methods.

environment

fall-back environment for evaluation of aesthetic symbols

assay.id

index of assay you are using when multiple assays exist.

Details

The biggest difference for objects returned by ggplot in ggbio from ggplot2, is we always keep the original data copy, this is useful because in ggbio, our starting point is not always data.frame, many special statistical transformation is computed upon original data objects instead of coerced data.frame. This is a hack to follow ggplot2's API while allow our own defined components to trace back to original data copy and do the transformation. For objects supported by mold we transform them to data.frame stored along the original data set, for objects which not supported by mold method, we only store the original copy for ggbio specific graphics.

ggplot() is typically used to construct a plot incrementally, using the + operator to add layers to the existing ggplot object. This is advantageous in that the code is explicit about which layers are added and the order in which they are added. For complex graphics with multiple layers, initialization with ggplot is recommended. You can always call qplot in package ggplot2 or autoplot in ggbio for convenient usage.

There are three common ways to invoke ggplot:

  • ggplot(df, aes(x, y, <other aesthetics>))

  • ggplot(df)

  • ggplot()

The first method is recommended if all layers use the same data and the same set of aesthetics, although this method can also be used to add a layer using data from another data frame. The second method specifies the default data frame to use for the plot, but no aesthetics are defined up front. This is useful when one data frame is used predominantly as layers are added, but the aesthetics may vary from one layer to another. The third method initializes a skeleton ggplot object which is fleshed out as layers are added. This method is useful when multiple data frames are used to produce different layers, as is often the case in complex graphics.

The examples below illustrate how these methods of invoking ggplot can be used in constructing a graphic.

Value

a return ggbio object, which is a subclass of ggplot defined in ggplot2 package, but that's more, a '.data' list entry is stored with the returned object.

Author(s)

Tengfei Yin

See Also

mold, ggbio

Examples

set.seed(1)
N <- 100
library(GenomicRanges)
## GRanges
gr <- GRanges(seqnames =
              sample(c("chr1", "chr2", "chr3"),
                     size = N, replace = TRUE),
              IRanges(
                      start = sample(1:300, size = N, replace = TRUE),
                      width = sample(70:75, size = N,replace = TRUE)),
              strand = sample(c("+", "-", "*"), size = N,
                replace = TRUE),
              value = rnorm(N, 10, 3), score = rnorm(N, 100, 30),
              sample = sample(c("Normal", "Tumor"),
                size = N, replace = TRUE),
              pair = sample(letters, size = N,
                replace = TRUE))

## automatically facetting and assign y
## this must mean geom_rect support GRanges object
ggplot(gr) + geom_rect()
ggplot(gr) + geom_alignment()
ggplot() + geom_alignment(gr)


## use pure ggplot2's geom_rect, no auto facet
ggplot(gr) + ggplot2::geom_rect(aes(xmin = start, ymin = score,
                               xmax = end, ymax = score + 1))

## GRangesList
grl <- split(gr, values(gr)$pair)
ggplot(grl) + geom_alignment()
ggplot(grl) + geom_rect()
ggplot(grl) + ggplot2::geom_rect(aes(xmin = start, ymin = score,
                               xmax = end, ymax = score + 1))


## IRanges
ir <- ranges(gr)
ggplot(ir) + geom_rect()
ggplot(ir) + layout_circle(geom = "rect")

## Seqinfo
seqlengths(gr) <- c(400, 500, 420)
ggplot(seqinfo(gr)) + geom_point(aes(x = midpoint, y = seqlengths))

## matrix
mx <- matrix(1:12, nrow = 3)
ggplot(mx, aes(x = x, y = y)) + geom_raster(aes(fill = value))
## row is the factor
ggplot(mx, aes(x = x, y = row)) + geom_raster(aes(fill = value))
colnames(mx)
colnames(mx) <- letters[1:ncol(mx)]
mx
## has extra 'colnames'
ggplot(mx, aes(x = x, y = row)) + geom_raster(aes(fill = colnames))
rownames(mx)
rownames(mx) <- LETTERS[1:nrow(mx)]
ggplot(mx, aes(x = x, y = row)) + geom_raster(aes(fill = rownames))
## please check autoplot, matrix for more control

##  Views










## ExpressionSet
library(Biobase)
data(sample.ExpressionSet)
sample.ExpressionSet
set.seed(1)
## select 50 features
idx <- sample(seq_len(dim(sample.ExpressionSet)[1]), size = 50)
eset <- sample.ExpressionSet[idx,]

ggplot(eset) + geom_tile(aes(x = x, y = y, fill = value))
## please check autoplot,matrix method which gives you more control
ggplot(eset) + geom_tile(aes(x = x, y = y, fill = sex))
ggplot(eset) + geom_tile(aes(x = x, y = y, fill = type))

## Rle
library(IRanges)
lambda <- c(rep(0.001, 4500), seq(0.001, 10, length = 500),
            seq(10, 0.001, length = 500))
xVector <- rpois(1e4, lambda)
xRle <- Rle(xVector)
ggplot(xRle) + geom_tile(aes(x = x, y = y, fill = value))

## RleList
xRleList <- RleList(xRle, 2L * xRle)
xRleList
ggplot(xRleList) + geom_tile(aes(x = x, y = y, fill = value)) +
facet_grid(group~.)
names(xRleList) <- c("a" ,"b")
ggplot(xRleList) + geom_tile(aes(x = x, y = y, fill = value)) +
facet_grid(group~.)


## RangedSummarizedExperiment
library(SummarizedExperiment)
nrows <- 200; ncols <- 6
counts <- matrix(runif(nrows * ncols, 1, 1e4), nrows)
counts2 <- matrix(runif(nrows * ncols, 1, 1e4), nrows)
rowRanges <- GRanges(rep(c("chr1", "chr2"), c(50, 150)),
                   IRanges(floor(runif(200, 1e5, 1e6)), width=100),
                   strand=sample(c("+", "-"), 200, TRUE))
colData <- DataFrame(Treatment=rep(c("ChIP", "Input"), 3),
                     row.names=LETTERS[1:6])
sset <- SummarizedExperiment(assays=SimpleList(counts=counts,
                                               counts2 = counts2),
                             rowRanges=rowRanges, colData=colData)
ggplot(sset) + geom_raster(aes(x = x, y = y , fill = value))

Results


R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(ggbio)
Loading required package: BiocGenerics
Loading required package: parallel

Attaching package: 'BiocGenerics'

The following objects are masked from 'package:parallel':

    clusterApply, clusterApplyLB, clusterCall, clusterEvalQ,
    clusterExport, clusterMap, parApply, parCapply, parLapply,
    parLapplyLB, parRapply, parSapply, parSapplyLB

The following objects are masked from 'package:stats':

    IQR, mad, xtabs

The following objects are masked from 'package:base':

    Filter, Find, Map, Position, Reduce, anyDuplicated, append,
    as.data.frame, cbind, colnames, do.call, duplicated, eval, evalq,
    get, grep, grepl, intersect, is.unsorted, lapply, lengths, mapply,
    match, mget, order, paste, pmax, pmax.int, pmin, pmin.int, rank,
    rbind, rownames, sapply, setdiff, sort, table, tapply, union,
    unique, unsplit

Loading required package: ggplot2
Need specific help about ggbio? try mailing 
 the maintainer or visit http://tengfei.github.com/ggbio/

Attaching package: 'ggbio'

The following objects are masked from 'package:ggplot2':

    geom_bar, geom_rect, geom_segment, ggsave, stat_bin, stat_identity,
    xlim

Warning message:
replacing previous import 'ggplot2::Position' by 'BiocGenerics::Position' when loading 'ggbio' 
> png(filename="/home/ddbj/snapshot/RGM3/R_BC/result/ggbio/ggplot-method.Rd_%03d_medium.png", width=480, height=480)
> ### Name: ggplot
> ### Title: ggplot methods
> ### Aliases: ggplot.Vector ggplot.Seqinfo ggplot.matrix
> ###   ggplot.ExpressionSet ggplot.RsamtoolsFile ggplot.character
> ###   ggplot.TxDbOREnsDb ggplot.BSgenome ggplot.SummarizedExperiment
> ###   ggplot.GAlignments ggplot.VCF
> 
> ### ** Examples
> 
> set.seed(1)
> N <- 100
> library(GenomicRanges)
Loading required package: S4Vectors
Loading required package: stats4

Attaching package: 'S4Vectors'

The following objects are masked from 'package:base':

    colMeans, colSums, expand.grid, rowMeans, rowSums

Loading required package: IRanges
Loading required package: GenomeInfoDb
> ## GRanges
> gr <- GRanges(seqnames =
+               sample(c("chr1", "chr2", "chr3"),
+                      size = N, replace = TRUE),
+               IRanges(
+                       start = sample(1:300, size = N, replace = TRUE),
+                       width = sample(70:75, size = N,replace = TRUE)),
+               strand = sample(c("+", "-", "*"), size = N,
+                 replace = TRUE),
+               value = rnorm(N, 10, 3), score = rnorm(N, 100, 30),
+               sample = sample(c("Normal", "Tumor"),
+                 size = N, replace = TRUE),
+               pair = sample(letters, size = N,
+                 replace = TRUE))
> 
> ## automatically facetting and assign y
> ## this must mean geom_rect support GRanges object
> ggplot(gr) + geom_rect()
> ggplot(gr) + geom_alignment()
> ggplot() + geom_alignment(gr)
> 
> 
> ## use pure ggplot2's geom_rect, no auto facet
> ggplot(gr) + ggplot2::geom_rect(aes(xmin = start, ymin = score,
+                                xmax = end, ymax = score + 1))
> 
> ## GRangesList
> grl <- split(gr, values(gr)$pair)
> ggplot(grl) + geom_alignment()
> ggplot(grl) + geom_rect()
> ggplot(grl) + ggplot2::geom_rect(aes(xmin = start, ymin = score,
+                                xmax = end, ymax = score + 1))
> 
> 
> ## IRanges
> ir <- ranges(gr)
> ggplot(ir) + geom_rect()
> ggplot(ir) + layout_circle(geom = "rect")
> 
> ## Seqinfo
> seqlengths(gr) <- c(400, 500, 420)
> ggplot(seqinfo(gr)) + geom_point(aes(x = midpoint, y = seqlengths))
> 
> ## matrix
> mx <- matrix(1:12, nrow = 3)
> ggplot(mx, aes(x = x, y = y)) + geom_raster(aes(fill = value))
> ## row is the factor
> ggplot(mx, aes(x = x, y = row)) + geom_raster(aes(fill = value))
> colnames(mx)
NULL
> colnames(mx) <- letters[1:ncol(mx)]
> mx
     a b c  d
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
> ## has extra 'colnames'
> ggplot(mx, aes(x = x, y = row)) + geom_raster(aes(fill = colnames))
> rownames(mx)
NULL
> rownames(mx) <- LETTERS[1:nrow(mx)]
> ggplot(mx, aes(x = x, y = row)) + geom_raster(aes(fill = rownames))
> ## please check autoplot, matrix for more control
> 
> ##  Views
> 
> ## ExpressionSet
> library(Biobase)
Welcome to Bioconductor

    Vignettes contain introductory material; view with
    'browseVignettes()'. To cite Bioconductor, see
    'citation("Biobase")', and for packages 'citation("pkgname")'.

> data(sample.ExpressionSet)
> sample.ExpressionSet
ExpressionSet (storageMode: lockedEnvironment)
assayData: 500 features, 26 samples 
  element names: exprs, se.exprs 
protocolData: none
phenoData
  sampleNames: A B ... Z (26 total)
  varLabels: sex type score
  varMetadata: labelDescription
featureData: none
experimentData: use 'experimentData(object)'
Annotation: hgu95av2 
> set.seed(1)
> ## select 50 features
> idx <- sample(seq_len(dim(sample.ExpressionSet)[1]), size = 50)
> eset <- sample.ExpressionSet[idx,]
> 
> ggplot(eset) + geom_tile(aes(x = x, y = y, fill = value))
> ## please check autoplot,matrix method which gives you more control
> ggplot(eset) + geom_tile(aes(x = x, y = y, fill = sex))
> ggplot(eset) + geom_tile(aes(x = x, y = y, fill = type))
> 
> ## Rle
> library(IRanges)
> lambda <- c(rep(0.001, 4500), seq(0.001, 10, length = 500),
+             seq(10, 0.001, length = 500))
> xVector <- rpois(1e4, lambda)
> xRle <- Rle(xVector)
> ggplot(xRle) + geom_tile(aes(x = x, y = y, fill = value))
> 
> ## RleList
> xRleList <- RleList(xRle, 2L * xRle)
> xRleList
RleList of length 2
[[1]]
integer-Rle of length 10000 with 829 runs
  Lengths:  729    1  208    1 1599    1  883 ...    1    1    1    5    1 4512
  Values :    0    1    0    1    0    1    0 ...    1    0    1    0    1    0

[[2]]
integer-Rle of length 10000 with 829 runs
  Lengths:  729    1  208    1 1599    1  883 ...    1    1    1    5    1 4512
  Values :    0    2    0    2    0    2    0 ...    2    0    2    0    2    0

> ggplot(xRleList) + geom_tile(aes(x = x, y = y, fill = value)) +
+ facet_grid(group~.)
> names(xRleList) <- c("a" ,"b")
> ggplot(xRleList) + geom_tile(aes(x = x, y = y, fill = value)) +
+ facet_grid(group~.)
> 
> 
> ## RangedSummarizedExperiment
> library(SummarizedExperiment)
> nrows <- 200; ncols <- 6
> counts <- matrix(runif(nrows * ncols, 1, 1e4), nrows)
> counts2 <- matrix(runif(nrows * ncols, 1, 1e4), nrows)
> rowRanges <- GRanges(rep(c("chr1", "chr2"), c(50, 150)),
+                    IRanges(floor(runif(200, 1e5, 1e6)), width=100),
+                    strand=sample(c("+", "-"), 200, TRUE))
> colData <- DataFrame(Treatment=rep(c("ChIP", "Input"), 3),
+                      row.names=LETTERS[1:6])
> sset <- SummarizedExperiment(assays=SimpleList(counts=counts,
+                                                counts2 = counts2),
+                              rowRanges=rowRanges, colData=colData)
> ggplot(sset) + geom_raster(aes(x = x, y = y , fill = value))
Assay index: 1 used
> 
> 
> 
> 
> 
> 
> dev.off()
null device 
          1 
>