Charts created by "ggpair" and "ggplot" side by side

Given the two ggplots, we can align them side by side

library(ggplot2) library(gridExtra) # Data data(iris) df <- data.frame(y = rnorm(100)) # Plotting p1 <- qplot(data=iris, Sepal.Width, Sepal.Length) p2 <- ggplot(df, aes(x=1:100, y=y)) + geom_line() grid.arrange(p1, p2, ncol=2) 

plot 1

How can we do this if one of the graphs is a ggpair object?

 library(GGally) p1 <- ggpairs(iris, colours='Species') p2 <- ggplot(df, aes(x=1:100, y=y)) + geom_line() grid.arrange(l, p2, ncol=6) # Error in gList(list(list(data = list(Sepal.Length = c(5.1, 4.9, 4.7, # 4.6, : only 'grobs' allowed in "gList" 
+4
source share
2 answers

To quickly create a mesh object from the ggpairs graph. Perhaps this is a little less reliable than the Roland method for writing a new ggpairs printing method, as in the help page ?grid.grab . * ... reliable replication of all possible output grids is not guaranteed. "(although using the wrap argument indicates that it should, it goes beyond my ken)

 library(ggplot2) library(grid) library(gridExtra) library(GGally) df <- data.frame(y = rnorm(100)) p1 <- ggpairs(iris, colours='Species') p2 <- ggplot(df, aes(x=1:100, y=y)) + geom_line() g <- grid.grabExpr(print(p1)) grid.arrange(g, p2, widths=c(0.8,0.2)) 

enter image description here

+4
source

First edit the print.ggpairs function print.ggpairs that it no longer grid.newpage . For this call

 library(GGally) fixInNamespace("print.ggpairs", ns = "GGally") 

and make line 32 a comment. Then you can use the grid functions:

 library(ggplot2) data(iris) df <- data.frame(y = rnorm(100)) p1 <- ggpairs(iris, colours='Species') p2 <- ggplot(df, aes(x=1:100, y=y)) + geom_line() library(grid) grid.newpage() pushViewport(viewport(layout=grid.layout(1,2))) vp1 <- viewport(layout.pos.col=1, layout.pos.row=1) vp2 <- viewport(layout.pos.col=2, layout.pos.row=1) pushViewport(vp1) p1 popViewport() pushViewport(vp2) plot(p2, vp = vp2) popViewport() 

final schedule

Edit: I sent a function request, and ggally supporter added a parameter to disable grid.newpage , that is, you can do print(p1, gridNewPage = FALSE) now: https://github.com/ggobi/ggally/issues/125

+8
source

Source: https://habr.com/ru/post/973163/


All Articles