【问题标题】:Using grid.arrange with multiple plots将 grid.arrange 与多个绘图一起使用
【发布时间】:2015-06-02 19:43:57
【问题描述】:

我正在将几个 ggparcoord(来自 GGally 包)子图绘制成一个大图。一般来说,除了一个子图之外,所有子图都来自同一个数据集 (x),而最后一个子图来自不同的数据集 (y)。

我希望每个子图都是不同的颜色。奇怪的是,当我不在 for 循环中执行它时,我可以让它工作,如下所示(在这种情况下,我有 3 个子图):

library(GGally)
library(ggplot2)
library(gridExtra)

set.seed(1)
colList = scales::hue_pal()(3)
plot_i = vector("list", length=2)

x = data.frame(a=runif(100,0,1),b=runif(100,0,1),c=runif(100,0,1),d=runif(100,0,1))
x$cluster = "color"
x$cluster2 = factor(x$cluster)
plot_i[[1]] = ggparcoord(x, columns=1:4, groupColumn=6, scale="globalminmax", alphaLines = 0.99) + xlab("Sample") + ylab("log(Count)") + scale_colour_manual(values = c("color" = colList[1]))

plot_i[[2]] = ggparcoord(x, columns=1:4, groupColumn=6, scale="globalminmax", alphaLines = 0.99) + xlab("Sample") + ylab("log(Count)") + scale_colour_manual(values = c("color" = colList[2]))

y = data.frame(a=runif(100,5,6),b=runif(100,5,6),c=runif(100,5,6),d=runif(100,5,6))
y$cluster = "color"
y$cluster2 = factor(y$cluster)
plot_i[[3]] = ggparcoord(y, columns=1:4, groupColumn=6, scale="globalminmax", alphaLines = 0.99) + xlab("Sample") + ylab("log(Count)") + scale_colour_manual(values = c("color" = colList[3]))


p = do.call("grid.arrange", c(plot_i, ncol=1)) 

但是,我试图自动化来自同一数据集 (x) 的所有子图,但遇到了困难。在上面的示例中,这只有 2 个子图。但我会增加这个数字。然而,无论如何,最后一个子图将始终来自另一个数据集 (y)。出于这个原因,我试图创建一个循环来遍历数据集 (x) 的许多子图。

library(ggplot2)
library(GGally)
library(gridExtra)

set.seed(1)
colList = scales::hue_pal()(3)
plot_1 = vector("list", length=2)
plot_2 = vector("list", length=1)
plot_1 <- lapply(1:2, function(i){
  x = data.frame(a=runif(100,0,1),b=runif(100,0,1),c=runif(100,0,1),d=runif(100,0,1))
  x$cluster = "color"
  x$cluster2 = factor(x$cluster)
  ggparcoord(x, columns=1:4, groupColumn=6, scale="globalminmax", alphaLines = 0.99) + xlab("Sample") + ylab("log(Count)") + theme(legend.position = "none", axis.title=element_text(size=12), axis.text=element_text(size=12)) + scale_colour_manual(values = c("color" = colList[i]))
})
p = do.call("grid.arrange", c(plot_1, ncol=1))

y = data.frame(a=runif(100,5,6),b=runif(100,5,6),c=runif(100,5,6),d=runif(100,5,6))
y$cluster = "color"
y$cluster2 = factor(y$cluster)
plot_2 = ggparcoord(y, columns=1:4, groupColumn=6, scale="globalminmax", alphaLines = 0.99) + xlab("Sample") + ylab("log(Count)") + theme(legend.position = "none", axis.title=element_text(size=12), axis.text=element_text(size=12)) + scale_colour_manual(values = c("color" = colList[3]))

p = do.call("grid.arrange", c(plot_1[[1]], plot_1[[2]], plot_2, ncol=1))

但是,我收到一个错误:

arrangeGrob(..., as.table = as.table, clip = clip, main = main, 中的错误: 输入必须是 grobs!

我尝试了与 (grid.arrange using list of plots) 类似的想法:

plist <- mget(c(plot_1[[1]], plot_1[[2]], plot_2))
do.call(grid.arrange, plist, ncol = 1)

并收到错误:

mget(c(plot_1[[1]], plot_1[[2]], plot_2)) 中的错误: 第一个参数无效

【问题讨论】:

    标签: r ggplot2 gridextra ggally


    【解决方案1】:

    唯一缺少的是,当您输入多个图时,它们需要在列表结构中。

    如果你更改了最后一行代码

    来自:

    p = do.call("grid.arrange", c(plot_1[[1]], plot_1[[2]], plot_2, ncol=1))
    

    到:

    p = do.call("grid.arrange", c(list(plot_1[[1]], plot_1[[2]], plot_2), ncol=1))
    

    我相信这会解决问题。

    【讨论】:

    • 这就像一个魅力。我想知道我是否可以在这里问:是否可以在没有在 plot_1[[1]] 和 plot_1[[2]] 中进行硬编码的情况下调用它?原因是,我不会总是在 plot_1 中有两个子图(尽管我在 plot_2 中总是有一个子图)。
    • 例如,我将定义一个变量 nPlots。在上面的示例中,nPlots = 2。我尝试了不会在数字 2 中明确硬编码的操作,例如: 1) p = do.call("grid.arrange", c(list(plot_clusters, plot_filtered), ncol =1)), 2) p = do.call("grid.arrange", c(list(paste0("plot_clusters[[", 1:nPlots, "]]", sep=","), plot_filtered), ncol=1))。
    【解决方案2】:
    library(ggplot2)
    library(GGally)
    library(gridExtra)
    
    set.seed(1)
    colList = scales::hue_pal()(3)
    nPlots = 3 #new code# - chose a random number for nPlots (3)
    #plot_1 = vector("list", length=nPlots) #new code# - length = nPlots
    #plot_2 = vector("list", length=1)
    plot_1 <- lapply(1:nPlots, function(i){ #new code# - 1:nPlots
      x = data.frame(a=runif(100,0,1),b=runif(100,0,1),c=runif(100,0,1),d=runif(100,0,1))
      x$cluster = "color"
      x$cluster2 = factor(x$cluster)
      ggparcoord(x, columns=1:4, groupColumn=6, scale="globalminmax", alphaLines = 0.99) + xlab("Sample") + ylab("log(Count)") + theme(legend.position = "none", axis.title=element_text(size=12), axis.text=element_text(size=12)) + scale_colour_manual(values = c("color" = colList[i]))
    })
    p = do.call("grid.arrange", c(plot_1, ncol=1))
    
    y = data.frame(a=runif(100,5,6),b=runif(100,5,6),c=runif(100,5,6),d=runif(100,5,6))
    y$cluster = "color"
    y$cluster2 = factor(y$cluster)
    plot_2 = ggparcoord(y, columns=1:4, groupColumn=6, scale="globalminmax", alphaLines = 0.99) + xlab("Sample") + ylab("log(Count)") + theme(legend.position = "none", axis.title=element_text(size=12), axis.text=element_text(size=12)) + scale_colour_manual(values = c("color" = colList[3]))
    
    p = do.call("grid.arrange", c(append(plot_1, list(plot_2)), ncol=1)) #new code#
    

    【讨论】:

    • 我在有变化的地方写了#new code#。注意 * 我注释掉了初始化 plot_1 和 plot_2 的位置。这不是必需的,因为您稍后会在代码中覆盖 plot_1 和 plot_2。但是,将其保留在代码中会显示您对这些对象的意图,这绝不是一件坏事。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-08
    • 2013-06-08
    相关资源
    最近更新 更多