在grid.arrange中使用layout_matrix时固定绘图区域宽度 [英] Fixing plot area width when using layout_matrix in grid.arrange

查看:318
本文介绍了在grid.arrange中使用layout_matrix时固定绘图区域宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在组合瓷砖的多面图.我希望每个图块都为正方形,或者至少采用相同的高度和宽度.

I am combining facet plots of tiles. I want each tile to be square, or at least take the same height and width.

到目前为止,我已经设法使用layout_matrix为每一行图块赋予相同的高度.尝试将每列图块的宽度固定为相等时(整个图)时,我陷入困境.

So far I have managed to give equal height to each row of tiles using layout_matrix. I am stuck when trying to fix an equal width to each column of tiles (across the plots).

一些基于mtcars的代码试图说明我的情节的布局(实际数据的方式更加复杂):

Some code based on mtcars to try and illustrate the layout of my plot (actual data way more complicated):

library("tidyverse")
library("gridExtra")

df0 <- mtcars %>% 
  group_by(cyl) %>%
  count()

df1 <- mtcars %>% 
  rownames_to_column("car") %>%
  mutate(man = gsub("([A-Za-z]+).*", "\\1", car))

g <- list()
for(i in 1:nrow(df0)){
  g[[i]] <- ggplot(data  =  df1 %>% filter(cyl == df0$cyl[i]), 
                   mapping = aes(x = "", y = car, fill = qsec)) +
    geom_tile() +
    facet_grid( man ~ .,  scales = "free_y", space = "free") +
    labs(x = "", y = "") +
    guides(fill = FALSE) +
    theme(strip.text.y = element_text(angle=0)) +
    coord_fixed()
}

m0 <- cbind(c(rep(1, df0$n[1]), rep(NA, max(df0$n) - df0$n[1])),
            c(rep(2, df0$n[2]), rep(NA, max(df0$n) - df0$n[2])),
            c(rep(3, df0$n[3]), rep(NA, max(df0$n) - df0$n[3])))
grid.arrange(grobs = g, layout_matrix =  m0)

这是哪个情节(减去我的MS Paint技能):

Which produces this plot (minus my MS Paint skills):

假定带状文本和y轴上标签的长度不同,导致绘图区域的宽度不同.不确定如何避免这种行为吗?我以为我可以在大型facet_grid上创建,但无法在上面的情节布局附近找到任何地方.

Presumably the different lengths of the labels in the strip text and y axis lead to the different widths for the plotting area. Not sure how I can avoid this behavior though? I thought I could create on big facet_grid but I could not get anywhere near the layout of the plot above.

推荐答案

事实证明,这是一件相当棘手的事情.幸运的是,cowplot::plot_grid已经可以进行对齐,从而导致列的大小相等.我只是采用了该功能并去除了绒毛,然后将其高度从通常使用的网格图案中分离出来.我们最后有了一个完成此任务的自定义函数(所有功劳归于Claus Wilke):

Turns out this is a rather tricky thing to do. Luckily, cowplot::plot_grid can already do the alignment that results in equal sizes of the columns. I just took that function and removed the fluff, and decoupled the heights from the grid pattern it normally uses. We end up with a little custom function that does the job (all credits to Claus Wilke):

plot_grid_gjabel <- function(plots, heights) {
  grobs <- lapply(plots, function(x) {
    if (!is.null(x)) 
      cowplot:::ggplot_to_gtable(x)
    else NULL
  })
  num_plots <- length(plots)
  num_widths <- unique(lapply(grobs, function(x) {
    length(x$widths)
  }))
  num_widths[num_widths == 0] <- NULL
  max_widths <- do.call(grid::unit.pmax, 
                        lapply(grobs, function(x) { x$widths }))
  for (i in 1:num_plots) {
    grobs[[i]]$widths <- max_widths
  }
  width <- 1 / num_plots
  height <- heights / max(heights)
  x <- cumsum(width[rep(1, num_plots)]) - width
  p <- cowplot::ggdraw() 
  for (i in seq_along(plots)) {
    p <- p + cowplot::draw_grob(grid::grobTree(grobs[[i]]), x[i], 1 - height[i], 
                                width, height[i])
  }
  return(p)
}

我们可以这样简单地称呼它:

We can simply call this like so:

plot_grid_gjabel(g, df0$n)

结果:

这篇关于在grid.arrange中使用layout_matrix时固定绘图区域宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆