在 'facet_grid'ed 和 'grid.arrange' 绘图上插入

piy*_*hra 3 r ggplot2

我正在尝试创建一个 2 面的 ggplot 对象,并将它们 grid.arrange 分成两列(grid.arrange 列)。在第二列中,在每个分面成员中,我想插入一个直方图。两列的数据来源也不同。到目前为止,我只能设法网格排列两个多面 ggplot 对象。我需要知道如何插入 grid.arrange 的第二列。

以下代码是我的数据的非常简单的表示。

#Two data sources (d1,d2). They will have even different facetting parameters as well, 
#but here I have simplified it.

d1<-data.frame(FacetRow=c(rep('a',4),rep('b',4)),
           FacetCol=rep(c(rep('c',2),rep('d',2)),2),
           X=rep(c(1,2),4),
           Y=runif(8)
           )
d2<-data.frame(FacetRow=c(rep('a',4),rep('b',4)),
               FacetCol=rep(c(rep('c',2),rep('d',2)),2),
               X=rep(c(1,2),4),
               Y=runif(8)
)

#GGPlot object for First column of grid.arrange
g.col.1<-ggplot(data=d1,aes(x=X,y=Y))+geom_line()+facet_grid(FacetRow~FacetCol)
#GGPlot object for Second column of grid.arrange
g.col.2<-ggplot(data=d2,aes(x=X,y=Y))+geom_line()+facet_grid(FacetRow~FacetCol)
grid.arrange(g.col.1,g.col.2,ncol=2)
Run Code Online (Sandbox Code Playgroud)

是它的样子。所以我只需要知道如何在每个 (e,f,g)X(h,i) 图中插入值的直方图。

此外,我需要通过改变 d1 和 d2 在 pdf 的不同页面上继续绘制它。所以我想我只能打印最终创建的ggplot对象一次,然后更改d1&d2然后再次打印,依此类推。

如果有人可以帮助解决这个问题,将不胜感激。

非常感谢

bap*_*ste 5

annotation_custom会很方便,不幸的是它并不意味着在不同的面板中放置不同的grob(不要问我)。这个最小的变化可以解决这个问题,

library(ggplot2)
library(gridExtra)
annotation_custom2 <- function (grob, xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf, data) 
  {
    layer(data = data, stat = StatIdentity, position = PositionIdentity, 
          geom = ggplot2:::GeomCustomAnn,
          inherit.aes = TRUE, params = list(grob = grob, 
                                            xmin = xmin, xmax = xmax, 
                                            ymin = ymin, ymax = ymax))
  }

inset <- qplot(rnorm(10))

p1 <- ggplot(data=d1,aes(x=X,y=Y))+geom_line()+facet_grid(FacetRow~FacetCol)
p2 <- ggplot(data=d2,aes(x=X,y=Y))+geom_line()+facet_grid(FacetRow~FacetCol) +
  annotation_custom2(ggplotGrob(inset), data=data.frame(FacetRow="b", FacetCol = "c", X=1, Y=1),
                     xmin=-Inf, xmax=1.5, ymin=-Inf, ymax=0.6)

grid.arrange(p1, p2, ncol=2)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

注意:您可以使用 plyr 自动生成这些注释层,例如

library(plyr)

insets <- dlply(d2, c("FacetRow", "FacetCol"), function(d){
  annotation_custom2(grob = ggplotGrob(qplot(d$Y) + 
                                         ggtitle(sprintf("col: %s\nrow: %s", 
                                                         unique(d$FacetCol), unique(d$FacetRow)))), 
                     data = data.frame(X=min(d$X), Y=min(d$Y), 
                                       FacetRow=unique(d$FacetRow), 
                                       FacetCol=unique(d$FacetCol)), 
                     xmin=-Inf, xmax=1.5, ymin=-Inf, ymax=0.6)
})

p1 <- ggplot(data=d1,aes(x=X,y=Y))+geom_line()+facet_grid(FacetRow~FacetCol)
p2 <- ggplot(data=d2,aes(x=X,y=Y))+geom_line()+facet_grid(FacetRow~FacetCol) +
  insets

grid.arrange(p1, p2, ncol=2)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明