【问题标题】:multiple different sized plots in same window in R?R中同一窗口中的多个不同大小的图?
【发布时间】:2021-11-02 02:27:44
【问题描述】:

类似的问题被问过很多次,例如herehere。但是,到目前为止我看到的所有其他答案并没有真正解决我的问题。

我试图在同一个窗口中绘制多个不同大小的图。我正在使用tidygraph 对象并使用ggraph 创建绘图。我尝试使用gridExtracowplots,但是我似乎无法让它们按我的意愿工作。

例如,首先我创建一些数据,将它们转换为 tidygraph 对象并将它们放在一个列表中,如下所示:

library(tidygraph)
library(ggraph)
library(cowplot)
library(gridExtra)


# create some data
edges  <- data.frame(from = c(1,1), to = c(2,3))
edges1 <- data.frame(from = c(1, 2, 2, 1, 5, 5), to = c(2, 3, 4, 5, 6, 7))
edges2 <- data.frame(from = c(1,2,2,1), to = c(2,3,4,5))

tg   <- tbl_graph(edges = edges)
tg_1 <- tbl_graph(edges = edges1)
tg_2 <- tbl_graph(edges = edges2)

myList <- list(tg, tg_1, tg_2)

然后我使用ggraph 创建一个绘图函数并创建一个绘图列表,如下所示:

# create plotting function
plotFun <- function(List, n) {
   plot <- ggraph(List, "partition") +
    geom_node_tile(aes(fill = "red"), size = 0.25) +
    scale_y_reverse() +
    theme_void() +
    theme(legend.position = "none")
}

# create list of all plots
allPlots <- lapply(myList, plotFun, n = length(myList))

因此,在我的示例中,我有 3 个不同的图...我试图在同一个窗口中绘制所有 3 个图,但每个图的大小不同。我想要的输出如下图所示...

我试图做的是有一个“全尺寸”地块,第二个地块是第一个地块的 1/2,第三个地块是第一个地块的 1/4。这看起来像:

注意:图片是用 Photoshop 制作的,所以我上面提到的比例在图片中并不准确。

我在尝试的解决方案中的想法是使用 gridExtracowplot...实际地块大小:

尝试的解决方案

# ATTEMPED COWPLOT SOLUTION
plot_grid(plotlist = allPlots, align = "hv", ncol = 2, rel_heights = c(1, 1/2, 1/4), rel_widths = c(1, 1/2, 1/4))


# ATTEMPED GRIDEXTRA SOLUTION
grid.arrange(
  grobs = allPlots,
  widths = c(1, 0.5, 1/4),
  heights = c(1, 0.5, 1/4),
  layout_matrix = rbind(c(1, 2, 3))
)

【问题讨论】:

    标签: r ggplot2 gridextra cowplot tidygraph


    【解决方案1】:

    编辑——固定为每个 OP 缩小第三张图表。

    library(patchwork)
    design <- c(         # we specify the top, left, bottom, and right
      area(1, 1, 4, 4),  # coordinates for each of the three plots
      area(1, 5, 2, 6),
      area(3, 5)
    )
    allPlots[[1]] +  allPlots[[2]] + allPlots[[3]] + 
      plot_layout(design = design)
    

    【讨论】:

      猜你喜欢
      • 2013-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-12
      • 1970-01-01
      相关资源
      最近更新 更多