While the original fortify() function in ggplot2 was used
to convert models and formulas to data.frames, it can also be used
as a hook to customise data prior to being put through the plotting
internals. Since ggplot exclusively handles data.frames, we
use fortify methods to convert S4 vector objects to base
data.frames.
# S3 method for Vector fortify(model, data, ...) # S3 method for DataFrame fortify(model, data, ...)
| model | model or other R object to convert to data frame |
|---|---|
| data | original dataset, if needed |
| ... | other arguments passed to methods |
A base R data.frame with (when applicable) S4 columns
The fortify methods in this package are written for two classes.
The Vector virtual class from which a broad
range of concrete classes in Bioconductor derive. The fortify method
column-binds the Vector content itself to the the result of
callling mcols() on the Vector and returns this as a base
data.frame. The column containing the Vector content will be
named .VectorClass, wherein VectorClass is the name of the
input's class.
The DataFrame class is converted to its
base R equivalent. Beware that any element metadata from the
Vectors in the DataFrame is not converted to seperate
columns.
Although S4 vectors can be converted to base R data.frames,
there is no guarantee that all functions tailored to data.frame will
work. The rbind method for data.frame for example, implicitly
converts columns to vectors. When there exists no such method for an S4
class, rbind will throw an error.
fortify for the original function in ggplot2.
# Element metadata of Vector classes are converted to columns x <- Rle(1:5) mcols(x) <- DataFrame(y = LETTERS[1:5]) fortify(x)#> .Rle y #> 1 1 A #> 2 2 B #> 3 3 C #> 4 4 D #> 5 5 E# Vector element metadata is not converted to a column when # fortifying a DataFrame df <- DataFrame(x = Rle(1:5), y = Factor(LETTERS[1:5])) mcols(df$x) <- DataFrame(z = "I'm Vector metadata") fortify(df)#> x y #> 1 1 A #> 2 2 B #> 3 3 C #> 4 4 D #> 5 5 E