### Draw and embed grob objects Source: https://cran.r-project.org/web/packages/ggplotify/vignettes/ggplotify.html Demonstrates how to draw grob objects using `grid.draw` and embed them within other plots using `pushViewport` and `viewport`. ```R grid.newpage() grid.draw(p1) vp = viewport(x=.35, y=.75, width=.35, height=.3) pushViewport(vp) grid.draw(p2) upViewport() ``` -------------------------------- ### Convert base R plots to grob objects Source: https://cran.r-project.org/web/packages/ggplotify/vignettes/ggplotify.html Use `as.grob` to convert base R plots generated by functions like `barplot`, `plot`, or `sin` into grob objects. It also supports plots from packages like `vcd` (e.g., `mosaic`) and `lattice` (e.g., `densityplot`). ```R library("grid") library("ggplotify") p1 <- as.grob(~barplot(1:10)) p2 <- as.grob(expression(plot(rnorm(10)))) p3 <- as.grob(function() plot(sin)) library("vcd") data(Titanic) p4 <- as.grob(~mosaic(Titanic)) library("lattice") data(mtcars) p5 <- as.grob(densityplot(~mpg|cyl, data=mtcars)) ``` -------------------------------- ### base2grob Source: https://cran.r-project.org/web/packages/ggplotify/refman/ggplotify.html Specifically converts base R graphics plots, defined by expressions or formulas, into 'grob' objects. ```APIDOC ## base2grob ### Description convert base plot to grob object ### Usage ```R base2grob(x, envir = parent.frame()) ``` ### Arguments - `x` (expression or formula or function) - 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) - environment to search variables ### Value grob object ### Author(s) Guangchuang Yu ### Examples ```R base2grob(~plot(rnorm(10))) ``` ``` -------------------------------- ### grid2grob Source: https://cran.r-project.org/web/packages/ggplotify/refman/ggplotify.html Converts a grid graphics plot call into a 'grob' object. ```APIDOC ## grid2grob ### Description convert grid plot to grob object ### Usage ```R grid2grob(plot_call) ``` ### Arguments - `plot_call` (call) - plot function call ### Value grob object ### Author(s) Guangchuang Yu ``` -------------------------------- ### as.grob Source: https://cran.r-project.org/web/packages/ggplotify/refman/ggplotify.html Converts various plot objects, including those from base R, ggplot2, lattice, and other packages, into 'grob' objects for use with the 'grid' package. ```APIDOC ## as.grob ### Description convert a plot to grob object ### Usage ```R 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, ...) ``` ### Arguments - `plot` (any) - base or grid plot, or graphic object generated by ggplot, lattice, etc. - `...` (any) - additional parameter, mostly will be ignored. ### Value grob object ### Author(s) Guangchuang Yu ### Examples ```R as.grob(~barplot(1:10)) ``` ``` -------------------------------- ### Convert grid plot to grob object Source: https://cran.r-project.org/web/packages/ggplotify/refman/ggplotify.html Use grid2grob to convert grid plot function calls into grob objects. ```R grid2grob(plot_call) ``` -------------------------------- ### as.ggplot Source: https://cran.r-project.org/web/packages/ggplotify/refman/ggplotify.html Converts a plot (base, grid, ggplot, lattice, etc.) into a ggplot object, facilitating its use within the ggplot2 framework. ```APIDOC ## as.ggplot ### Description convert plot to ggplot object ### Usage ```R as.ggplot(plot, scale = 1, hjust = 0, vjust = 0, angle = 0, ...) ``` ### Arguments - `plot` (any) - base or grid plot, or graphic generated by ggplot, lattice, etc. - `scale` (numeric) - scale of the plot to be drawn - `hjust` (numeric) - horizontal adjustment - `vjust` (numeric) - vertical adjustment - `angle` (numeric) - angle to rotate plot - `...` (any) - additional parameters passed to as.grob ### Value ggplot object ### Author(s) Guangchuang Yu ### Examples ```R as.ggplot(~barplot(1:10)) ``` ``` -------------------------------- ### Align plots using cowplot Source: https://cran.r-project.org/web/packages/ggplotify/vignettes/ggplotify.html This snippet shows how to combine plots converted to ggplot objects, including base R plots and ggplot2 plots, using the `plot_grid` function from the `cowplot` package for alignment and arrangement. ```R library(cowplot) library(colorspace) col <- rainbow_hcl(3) names(col) <- unique(iris$Species) color <- col[iris$Species] p6 <- as.ggplot(~plot(iris$Sepal.Length, iris$Sepal.Width, col=color, pch=15)) p7 <- ggplot(iris, aes(Sepal.Length, Sepal.Width, color=Species)) + geom_point(shape=15) + scale_color_manual(values=col, name="") legend <- get_legend(p7) ## also able to annotate base or other plots using ggplot2 library(ggimage) p8 <- p6 + geom_subview(x=.7, y=.78, subview=legend) p9 <- as.ggplot(~image(volcano)) plot_grid(p1, p2, p3, p4, p5, p6, p7, p8, p9, ncol=3, labels=LETTERS[1:9]) ``` -------------------------------- ### Convert base plot to grob object Source: https://cran.r-project.org/web/packages/ggplotify/refman/ggplotify.html Use base2grob to convert base R plot expressions or formulas into grob objects. It can also handle functions that generate plots when called. ```R base2grob(~plot(rnorm(10))) ``` -------------------------------- ### Convert base plot to ggplot object Source: https://cran.r-project.org/web/packages/ggplotify/refman/ggplotify.html Use as.ggplot to convert base R plots, grid plots, or plots from other libraries into ggplot objects. This is useful for integrating them into the ggplot2 framework. ```R as.ggplot(~barplot(1:10)) ``` -------------------------------- ### Convert plot to grob object Source: https://cran.r-project.org/web/packages/ggplotify/refman/ggplotify.html as.grob is a generic function to convert various plot objects, including expressions, formulas, ggplot objects, and plots from libraries like lattice or pheatmap, into grob objects. ```R as.grob(~barplot(1:10)) ``` -------------------------------- ### Convert plots to ggplot objects Source: https://cran.r-project.org/web/packages/ggplotify/vignettes/ggplotify.html The `as.ggplot` function converts various plot types, including base R plots, expressions, and functions, into ggplot objects. This allows for further annotation using ggplot2's capabilities. ```R library(ggplot2) p1 <- as.ggplot(~barplot(1:10)) + annotate("text", x = .6, y = .5, label = "Hello Base Plot", size = 5, color = 'firebrick', angle=45) p2 <- as.ggplot(expression(plot(rnorm(10)))) p3 <- as.ggplot(function() plot(sin)) p4 <- as.ggplot(~mosaic(Titanic)) p5 <- as.ggplot(densityplot(~mpg|cyl, data=mtcars)) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.