4

我想安排我的ggpairs情节arrangeGrob

library(GGally)
library(gridExtra)

df <- structure(list(var1 = 1:5, var2 = 4:8, var3 = 6:10), .Names = c("var1", "var2", "var3"), row.names = c(NA, -5L), class = "data.frame")

p1 <- ggpairs(df, 1:3) 
p2 <- ggpairs(df, 1:2)

p <- arrangeGrob(p1, p2, ncol=2)

导致此错误:

Error in arrangeGrob(p1, p2, ncol = 2) : input must be grobs!

有没有办法解决这个问题?

4

3 回答 3

3

不幸的是,我看不出这是怎么可能的。

首先,它不适用于gridExtra,因为arrangeGrobTableGrob对象进行操作:

> ggplotGrob(qplot(1:100))
stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.
TableGrob (6 x 5) "layout": 8 grobs
  z     cells       name                                 grob
1 0 (1-6,1-5) background      rect[plot.background.rect.3349]
2 3 (3-3,3-3)     axis-l absoluteGrob[GRID.absoluteGrob.3341]
3 1 (4-4,3-3)     spacer                       zeroGrob[NULL]
4 2 (3-3,4-4)      panel               gTree[GRID.gTree.3329]
5 4 (4-4,4-4)     axis-b absoluteGrob[GRID.absoluteGrob.3335]
6 5 (5-5,4-4)       xlab         text[axis.title.x.text.3343]
7 6 (3-3,2-2)       ylab         text[axis.title.y.text.3345]
8 7 (2-2,4-4)      title           text[plot.title.text.3347]

object的内部表示ggpairs只包含要调用的方法,而不是实际的 grobs:

> str(p1$plots)
List of 9
 $ : chr "ggally_diagAxis(ggally_data, ggplot2::aes(x = var1))"
 $ : chr "ggally_cor(ggally_data, ggplot2::aes(x = var2, y = var1))"
 $ : chr "ggally_cor(ggally_data, ggplot2::aes(x = var3, y = var1))"
 $ : chr "ggally_points(ggally_data, ggplot2::aes(x = var1, y = var2))"
 $ : chr "ggally_diagAxis(ggally_data, ggplot2::aes(x = var2))"
 $ : chr "ggally_cor(ggally_data, ggplot2::aes(x = var3, y = var2))"
 $ : chr "ggally_points(ggally_data, ggplot2::aes(x = var1, y = var3))"
 $ : chr "ggally_points(ggally_data, ggplot2::aes(x = var2, y = var3))"
 $ : chr "ggally_diagAxis(ggally_data, ggplot2::aes(x = var3))"

构建实际 grobs 的整个工作由GGally:::print.ggpairs. 看一下源代码,你会看到grid.newpage()多个popViewports 和pushViewports。这意味着即使是低级grid方法(例如此处描述的)也不适用,因为您的grid结构将被GGally:::print.ggpairs.

当然,总有办法。修改源代码始终是一种选择,尽管我相信这对您的任务来说是一条漫长而艰难的道路。如果有人能想出一个简单的解决方法,我会很高兴被证明是错误的。

于 2014-06-10T10:05:37.980 回答
3
library(GGally)
library(gridExtra)
library(grid)

# reproducing initial data
df <- structure(list(var1 = 1:5, var2 = 4:8, var3 = 6:10), 
                .Names = c("var1", "var2", "var3"), 
                row.names = c(NA, -5L), class = "data.frame")
p1 <- ggpairs(df, 1:3) 
p2 <- ggpairs(df, 1:2)

# declaring new method for drawing ggmatrix objects using grid.draw
grid.draw.ggmatrix <- function(x, recording = T) print(x, gridNewPage = F)
# then making them look like grob objects
attributes(p1)$class <- c(attributes(p1)$class, "grob")
attributes(p2)$class <- c(attributes(p2)$class, "grob")

p <- arrangeGrob(p1, p2, ncol=2)
grid.newpage()
grid.draw(p)

排列的 ggpairs 图

于 2016-02-03T08:29:58.660 回答
1

刚刚找到了如何使用 cowplot 解决这个问题的答案(在 https://github.com/ggobi/ggally/issues/256找到)

解决方案:

 library(GGally)
 library(gridExtra)
 library(cowplot)

 df <- structure(list(var1 = 1:5,var2 = 4:8, var3 = 6:10), 
                      .Names = c("var1",  "var2", "var3"),
                      row.names = c(NA, -5L), class = "data.frame")

 p1 <- ggpairs(df, 1:3) 
 p2 <- ggpairs(df, 1:2)



 plot_grid(
   ggmatrix_gtable(p1),
   ggmatrix_gtable(p2),
   nrow = 2)

于 2021-06-01T10:22:13.093 回答