### Install ggplotify Development Version Source: https://github.com/guangchuangyu/ggplotify/blob/master/README.md Install the latest development version of the ggplotify package directly from GitHub using the devtools package. ```r devtools::install_github("GuangchuangYu/ggplotify") ``` -------------------------------- ### Install ggplotify from CRAN Source: https://github.com/guangchuangyu/ggplotify/blob/master/README.md Use this command to install the stable version of the ggplotify package from the Comprehensive R Archive Network (CRAN). ```r install.packages("ggplotify") ``` -------------------------------- ### Combine Plots from Different Systems using cowplot Source: https://context7.com/guangchuangyu/ggplotify/llms.txt This example demonstrates combining plots from various graphics systems (base R, ggplot2, lattice) into a single aligned multi-panel figure using cowplot's plot_grid(). All plots are converted to ggplot objects first for compatibility. ```r library(ggplotify) library(ggplot2) library(cowplot) library(lattice) library(vcd) library(colorspace) # Create plots from different systems p1 <- as.ggplot(~barplot(1:10)) + annotate("text", x = 0.6, y = 0.5, label = "Base Plot", size = 4, color = 'firebrick') p2 <- as.ggplot(expression(plot(rnorm(10)))) p3 <- as.ggplot(function() plot(sin)) # vcd mosaic plot data(Titanic) p4 <- as.ggplot(~mosaic(Titanic)) # lattice density plot p5 <- as.ggplot(densityplot(~mpg|cyl, data = mtcars)) # Base scatter plot with custom colors 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)) # Native ggplot p7 <- ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) + geom_point(shape = 15) + scale_color_manual(values = col, name = "") # Image plot p8 <- as.ggplot(~image(volcano)) # Combine all plots into a grid combined <- plot_grid(p1, p2, p3, p4, p5, p6, p7, p8, ncol = 3, labels = LETTERS[1:8]) print(combined) # Extract legend from ggplot and add to base plot legend <- get_legend(p7) library(ggimage) p9 <- p6 + geom_subview(x = 0.7, y = 0.78, subview = legend) print(p9) ``` -------------------------------- ### Capture Complex Grid Composition Source: https://context7.com/guangchuangyu/ggplotify/llms.txt This snippet demonstrates capturing a complex grid composition using grid2grob. It sets up viewports and draws rectangles within them. ```r p3 <- grid2grob({ pushViewport(viewport(layout = grid.layout(2, 2))) pushViewport(viewport(layout.pos.row = 1, layout.pos.col = 1)) grid.rect(gp = gpar(fill = "red")) popViewport() pushViewport(viewport(layout.pos.row = 1, layout.pos.col = 2)) grid.rect(gp = gpar(fill = "blue")) popViewport() popViewport() }) grid.newpage() grid.draw(p1) ``` -------------------------------- ### Convert Grid Graphics to grob Source: https://context7.com/guangchuangyu/ggplotify/llms.txt Capture grid graphics commands and convert them into a grob object using `grid2grob()`. ```r library(ggplotify) library(grid) # Capture grid graphics as grob p1 <- grid2grob({ grid.rect(gp = gpar(fill = "lightblue")) grid.text("Hello Grid", gp = gpar(fontsize = 20)) }) ``` -------------------------------- ### Use Custom Environment for base2grob Source: https://context7.com/guangchuangyu/ggplotify/llms.txt Provide a custom environment to `base2grob()` for variable lookup when converting base R plots defined within specific scopes. ```r library(ggplotify) library(grid) # Use with custom environment for variable lookup my_data <- 1:20 p5 <- base2grob(~plot(my_data), envir = environment()) ``` -------------------------------- ### Draw Expression Directly with grid.draw Source: https://context7.com/guangchuangyu/ggplotify/llms.txt Shows how to draw an expression directly using grid.draw. This allows for deferred execution of plot commands. ```r # Draw expression directly grid.newpage() grid.draw(expression(plot(sin))) ``` -------------------------------- ### Draw Formula Directly with grid.draw Source: https://context7.com/guangchuangyu/ggplotify/llms.txt Demonstrates drawing a formula directly using grid.draw, which is a convenient way to use base plot calls within the grid system. ```r library(ggplotify) library(grid) # Draw formula directly with grid.draw grid.newpage() grid.draw(~barplot(1:10)) ``` -------------------------------- ### Draw grob Object with grid System Source: https://context7.com/guangchuangyu/ggplotify/llms.txt Draw a grob object, previously created with `as.grob()`, using `grid.draw()` within the grid graphics system. ```r library(ggplotify) library(grid) # Draw grob using grid system grid.newpage() grid.draw(p1) ``` -------------------------------- ### Draw Function Directly with grid.draw Source: https://context7.com/guangchuangyu/ggplotify/llms.txt Illustrates drawing an anonymous function directly using grid.draw. This is useful for plots that might have side effects or require specific device states. ```r # Draw function directly grid.newpage() grid.draw(function() hist(rnorm(100))) ``` -------------------------------- ### Convert Lattice Print Output to grob Source: https://context7.com/guangchuangyu/ggplotify/llms.txt Use `grid2grob()` to convert the output of a lattice plot's print method into a grob object. ```r library(ggplotify) library(grid) library(lattice) p2 <- grid2grob(print(densityplot(~mpg, data = mtcars))) ``` -------------------------------- ### Convert Base Plot to grob Object Source: https://context7.com/guangchuangyu/ggplotify/llms.txt Use `as.grob()` with formula, expression, or function syntax to convert base R plots into grob objects compatible with the grid graphics system. ```r library(ggplotify) library(grid) # Convert base plot using formula p1 <- as.grob(~barplot(1:10)) # Convert using expression p2 <- as.grob(expression(plot(rnorm(10)))) # Convert using function p3 <- as.grob(function() plot(sin)) ``` -------------------------------- ### Convert ggplot Object to grob Source: https://context7.com/guangchuangyu/ggplotify/llms.txt Convert an existing ggplot object to a grob object using `as.grob()` for compatibility with the grid system. ```r library(ggplotify) library(grid) library(ggplot2) gg <- ggplot(mtcars, aes(mpg, wt)) + geom_point() grob_from_ggplot <- as.grob(gg) ``` -------------------------------- ### Convert Lattice Plot to grob Object Source: https://context7.com/guangchuangyu/ggplotify/llms.txt Convert lattice (trellis) plot objects to grob objects using `as.grob()`. ```r library(ggplotify) library(grid) library(lattice) p5 <- as.grob(densityplot(~mpg|cyl, data=mtcars)) ``` -------------------------------- ### Convert Base Plot to grob using base2grob Source: https://context7.com/guangchuangyu/ggplotify/llms.txt Specifically convert base R graphics to grob objects using `base2grob()`, accepting formula, expression, or function inputs. ```r library(ggplotify) library(grid) # Convert base plot using formula p1 <- base2grob(~plot(rnorm(10))) # Convert using expression p2 <- base2grob(expression(pie(1:5))) # Convert using function p3 <- base2grob(function() { hist(rnorm(100), main = "Histogram") }) ``` -------------------------------- ### Convert vcd Mosaic Plot to grob Source: https://context7.com/guangchuangyu/ggplotify/llms.txt Convert a vcd mosaic plot to a grob object using `as.grob()` for integration with the grid system. ```r library(ggplotify) library(grid) library(vcd) data(Titanic) p4 <- as.grob(~mosaic(Titanic)) ``` -------------------------------- ### Draw Base Plot grob with grid Source: https://context7.com/guangchuangyu/ggplotify/llms.txt Draw a grob object created from a base R plot using `base2grob()` with `grid.draw()`. ```r library(ggplotify) library(grid) # Draw with grid grid.newpage() grid.draw(p1) ``` -------------------------------- ### Embed grob within Viewports Source: https://context7.com/guangchuangyu/ggplotify/llms.txt Embed one grob object within a specific viewport of another plot using `pushViewport()` and `grid.draw()`. ```r library(ggplotify) library(grid) # Embed plot inside another plot using viewports grid.newpage() grid.draw(p1) vp <- viewport(x = 0.35, y = 0.75, width = 0.35, height = 0.3) pushViewport(vp) grid.draw(p2) upViewport() ``` -------------------------------- ### Convert Lattice Plot to ggplot Object Source: https://context7.com/guangchuangyu/ggplotify/llms.txt Directly convert lattice plot objects to ggplot objects using `as.ggplot()` for integration into the ggplot2 workflow. ```r library(ggplotify) library(ggplot2) library(lattice) lattice_plot <- densityplot(~mpg|cyl, data=mtcars) p4 <- as.ggplot(lattice_plot) print(p4) ``` -------------------------------- ### Convert Base Plot to ggplot Object Source: https://context7.com/guangchuangyu/ggplotify/llms.txt Use `as.ggplot()` with formula, expression, or anonymous function syntax to convert base R plots into ggplot objects for further modification. ```r library(ggplotify) library(ggplot2) # Convert base plot using formula syntax p1 <- as.ggplot(~barplot(1:10)) print(p1) # Convert using expression syntax p2 <- as.ggplot(expression(plot(rnorm(10)))) print(p2) # Convert using anonymous function p3 <- as.ggplot(function() plot(sin)) print(p3) ``` -------------------------------- ### Add ggplot2 Annotations to Converted Base Plot Source: https://context7.com/guangchuangyu/ggplotify/llms.txt After converting a base plot to a ggplot object using `as.ggplot()`, you can add standard ggplot2 annotations. ```r library(ggplotify) library(ggplot2) p5 <- as.ggplot(~barplot(1:10)) + annotate("text", x = 0.6, y = 0.5, label = "Hello Base Plot", size = 5, color = 'firebrick', angle = 45) print(p5) ``` -------------------------------- ### Convert Complex Base Plot to grob Source: https://context7.com/guangchuangyu/ggplotify/llms.txt Use `base2grob()` to convert complex base R plots involving multiple plotting commands and legends into grob objects. ```r library(ggplotify) library(grid) # Complex base plot p4 <- base2grob(~{ par(mfrow = c(1, 1)) plot(iris$Sepal.Length, iris$Sepal.Width, col = as.numeric(iris$Species), pch = 15, main = "Iris Data") legend("topright", legend = levels(iris$Species), col = 1:3, pch = 15) }) ``` -------------------------------- ### Scale and Position Converted Plot Source: https://context7.com/guangchuangyu/ggplotify/llms.txt Adjust the scale, horizontal justification (hjust), and vertical justification (vjust) of a converted plot using `as.ggplot()` arguments. ```r library(ggplotify) library(ggplot2) p6 <- as.ggplot(~plot(1:10), scale = 0.8, hjust = 0.1, vjust = 0.1) print(p6) ``` -------------------------------- ### Rotate Converted Plot Source: https://context7.com/guangchuangyu/ggplotify/llms.txt Rotate a converted plot by specifying an angle in degrees using the `angle` argument in `as.ggplot()`. ```r library(ggplotify) library(ggplot2) p7 <- as.ggplot(~barplot(1:5), angle = 45, scale = 0.7) print(p7) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.