grobs的定位

grobs的定位,r,ggplot2,grid,grob,R,Ggplot2,Grid,Grob,我想使用arrangeGrob在主图和效应图(森林图)中将线性模型的数据绘制为子图 以下是数据: set.seed(1) main.df <- data.frame(sample=c(paste("E.plus.A.plus",1:3,sep="_"),paste("E.minus.A.plus",1:3,sep="_"),paste("E.plus.A.minus",1:3,sep="_"),paste("E.minus.A.minus",1:3,sep="_")),

我想使用
arrangeGrob
在主图和效应图(森林图)中将线性模型的数据绘制为子图

以下是数据:

set.seed(1)
main.df <- data.frame(sample=c(paste("E.plus.A.plus",1:3,sep="_"),paste("E.minus.A.plus",1:3,sep="_"),paste("E.plus.A.minus",1:3,sep="_"),paste("E.minus.A.minus",1:3,sep="_")),
                      replicate=rep(1:3,4),cpm=c(rnorm(12)),
                      factor.level=factor(c(rep("E.plus.A.plus",3),rep("E.minus.A.plus",3),rep("E.plus.A.minus",3),rep("E.minus.A.minus",3)),
                                    levels=c("E.plus.A.plus","E.minus.A.plus","E.plus.A.minus","E.minus.A.minus")))

effects.df <- data.frame(factor.level=c("E.plus.A.plus-E.minus.A.plus","E.plus.A.plus-E.plus.A.minus","E.plus.A.plus-E.minus.A.minus",
                                        "E.minus.A.plus-E.plus.A.minus","E.minus.A.plus-E.minus.A.minus","E.plus.A.minus-E.minus.A.minus"),
                         effect=rnorm(6),effect.df=runif(6,0,0.5),p.value=runif(6,0,1),y=1:6+0.2)
effects.df$effect.high <- effects.df$effect+effects.df$effect.df
effects.df$effect.low <- effects.df$effect-effects.df$effect.df
effects.df$factor.level <- factor(effects.df$factor.level,levels=effects.df$factor.level)
set.seed(1)

main.df对于此类任务,我强烈建议使用包
cowplot
。在这里,我构建了三个嵌套集(主图位于左侧,然后两个图例位于右上角,然后子图位于右下角)。请注意奇妙的
get_legend
功能,它使绘制图例变得非常简单

plot_grid(
  main.plot + theme(legend.position = "none")
  , plot_grid(
    plot_grid(
      get_legend(main.plot)
      , get_legend(sub.plot)
      , nrow = 1
    )
    , sub.plot + theme(legend.position = "none")
    , nrow = 2
  )
  , nrow = 1
  )
给出:

显然,我建议更改其中一个(或两个)调色板,但这会满足您的需要

如果您确实希望图例与sub.plot一起使用,而不是与其他图例一起使用,则可以跳过
get_legend

如果您想要的不是偶数大小,还可以使用
rel_widths
rel_heights
调整集合的宽度/高度


作为补充说明,
cowplot
在加载时设置自己的默认主题。我通常会在加载后立即运行
theme\u set(theme\u minimal())
恢复到我喜欢的状态。

这里有一个grid.arrange解决方案

grid.arrange(grobs = replicate(4, ggplot(), simplify = FALSE), 
             layout_matrix = cbind(c(1,1), c(3,2), c(4, 2)), 
             widths = c(2,1,1))

有了这些零碎的东西

get_legend <- function(p) {
   g <- ggplotGrob(p)
   id <- grep("guide", g$layout$name)
   g$grobs[[id]]
}

leg1 <- get_legend(main.plot); leg2 <- get_legend(sub.plot)
gl <- list(main.plot + theme(legend.position = "none"), 
           sub.plot + theme(legend.position = "none"), leg1, leg2)

grid.arrange(grobs = gl, 
             layout_matrix = cbind(c(1,1), c(3,2), c(4, 2)), 
             widths = c(2,1,1))

获取一个简短的注释。你到底为什么需要传说?变量似乎由y轴上的不同面或位置很好地表示。顺便说一句,你见过和吗?
plot_grid(
  main.plot + theme(legend.position = "none")
  , plot_grid(
    plot_grid(
      get_legend(main.plot)
      , get_legend(sub.plot)
      , nrow = 1
    )
    , sub.plot + theme(legend.position = "none")
    , nrow = 2
  )
  , nrow = 1
  )
grid.arrange(grobs = replicate(4, ggplot(), simplify = FALSE), 
             layout_matrix = cbind(c(1,1), c(3,2), c(4, 2)), 
             widths = c(2,1,1))
get_legend <- function(p) {
   g <- ggplotGrob(p)
   id <- grep("guide", g$layout$name)
   g$grobs[[id]]
}

leg1 <- get_legend(main.plot); leg2 <- get_legend(sub.plot)
gl <- list(main.plot + theme(legend.position = "none"), 
           sub.plot + theme(legend.position = "none"), leg1, leg2)

grid.arrange(grobs = gl, 
             layout_matrix = cbind(c(1,1), c(3,2), c(4, 2)), 
             widths = c(2,1,1))