Package {ggplotify}


Title: Convert Almost Any Plot to a 'grob' or 'ggplot' Object
Version: 0.1.4
Description: Convert a plot to a 'grob' or a 'ggplot' object so that it joins the 'grid' and 'ggplot2' ecosystem. The input can be a plot call written as a formula or an expression, a function that draws to a graphics device, a graphic object produced by another package, or a plot captured by 'grDevices::recordPlot'. Output of 'base' graphics is redrawn with 'gridGraphics', so plots from different graphics systems can be arranged, aligned, annotated and inset with packages such as 'aplot' and 'patchwork'.
Depends: R (≥ 3.4.0)
Imports: ggplot2, graphics, grDevices, grid, gridGraphics, rlang, utils, yulab.utils
Suggests: aplot, colorspace, ggimage, knitr, lattice, patchwork, quarto, testthat, vcd
VignetteBuilder: quarto
ByteCompile: true
License: Artistic-2.0
URL: https://github.com/GuangchuangYu/ggplotify
BugReports: https://github.com/GuangchuangYu/ggplotify/issues
Encoding: UTF-8
Config/testthat/edition: 3
Config/roxygen2/version: 8.1.0
NeedsCompilation: no
Packaged: 2026-10-12 06:50:28 UTC; wang
Author: Guangchuang Yu ORCID iD [aut, cre]
Maintainer: Guangchuang Yu <guangchuangyu@gmail.com>
Repository: CRAN
Date/Publication: 2026-10-12 13:40:02 UTC

ggplotify: Convert Almost Any Plot to a 'grob' or 'ggplot' Object

Description

Convert a plot to a 'grob' or a 'ggplot' object so that it joins the 'grid' and 'ggplot2' ecosystem. The input can be a plot call written as a formula or an expression, a function that draws to a graphics device, a graphic object produced by another package, or a plot captured by 'grDevices::recordPlot'. Output of 'base' graphics is redrawn with 'gridGraphics', so plots from different graphics systems can be arranged, aligned, annotated and inset with packages such as 'aplot' and 'patchwork'.

Author(s)

Maintainer: Guangchuang Yu guangchuangyu@gmail.com (ORCID)

Authors:

See Also

Useful links:


as.ggplot

Description

convert plot to ggplot object

Usage

as.ggplot(plot, scale = 1, hjust = 0, vjust = 0, angle = 0, ...)

Arguments

plot

base or grid plot, or graphic generated by ggplot, lattice, etc.

scale

the plot is drawn into the rectangle that runs from 1 - scale to scale on both axes, and is clipped to it. scale = 1 fills the panel; a small scale cuts off titles and axis labels rather than shrinking them. When angle is not 0 this argument is the width of the rotated viewport instead.

hjust

horizontal shift of that rectangle, in panel units

vjust

vertical shift of that rectangle, in panel units

angle

angle to rotate plot

...

additional parameters passed to as.grob

Value

ggplot object

Author(s)

Guangchuang Yu

Examples

as.ggplot(~barplot(1:10))

as.grob

Description

convert a plot to grob object

Usage

as.grob(plot, ...)

## S3 method for class 'aplot'
as.grob(plot, ...)

## S3 method for class 'oncoplot'
as.grob(plot, ...)

## S3 method for class 'bbplot'
as.grob(plot, ...)

## S3 method for class 'patchwork'
as.grob(plot, ...)

## S3 method for class 'gglist'
as.grob(plot, ...)

## S3 method for class 'expression'
as.grob(plot, ...)

## S3 method for class 'formula'
as.grob(plot, ...)

## S3 method for class ''function''
as.grob(plot, ...)

## S3 method for class 'ggplot'
as.grob(plot, ...)

## S3 method for class 'meme'
as.grob(plot, ...)

## S3 method for class 'trellis'
as.grob(plot, ...)

## S3 method for class 'eulergram'
as.grob(plot, ...)

## S3 method for class 'Heatmap'
as.grob(plot, ...)

## S3 method for class 'upset'
as.grob(plot, ...)

## S3 method for class 'tmap'
as.grob(plot, ...)

## S3 method for class 'pheatmap'
as.grob(plot, ...)

## S3 method for class 'grob'
as.grob(plot, ...)

## S3 method for class 'recordedplot'
as.grob(plot, ...)

Arguments

plot

base or grid plot, or graphic object generated by ggplot, lattice, etc. A plot recorded by grDevices::recordPlot() is also accepted.

...

additional parameter, mostly will be ignored.

Details

A recordedplot object holds the display list of the device that drew the plot, so the display list must be enabled, via grDevices::dev.control(displaylist = "enable"), before the plot is drawn. A recording that holds no graphics, which is what an inhibited device produces, cannot be converted and is reported as an error. Replaying a recording also requires the same R version, graphics device and packages that produced it, and is only guaranteed within the R session where recordPlot() was called.

A formula or an expression is evaluated in the environment that as.grob() itself was called from, which is the global environment at the top level. A variable that is local to the function calling as.grob() is not visible there and needs envir, as in as.grob(~barplot(n), envir = environment()).

Value

grob object

Author(s)

Guangchuang Yu

Examples

as.grob(~barplot(1:10))

grDevices::pdf(tempfile(fileext = ".pdf"))
grDevices::dev.control(displaylist = "enable")
plot(1:10, type = "b")
rec <- grDevices::recordPlot()
grDevices::dev.off()

g <- as.grob(rec)

base2grob

Description

convert base plot to grob object

Usage

base2grob(x, envir = parent.frame())

Arguments

x

expression or formula of base plot function call, e.g. expression(pie(1:5)) or ~plot(1:10, 1:10); or a function that plots to an R graphics device when called, e.g. function() plot(sqrt)

envir

environment to search variables

Value

grob object

Author(s)

Guangchuang Yu

Examples

base2grob(~plot(rnorm(10)))

grid2grob

Description

convert grid plot to grob object

Usage

grid2grob(plot_call)

Arguments

plot_call

plot function call

Value

grob object

Author(s)

Guangchuang Yu