使用带有多个图的grid.arrange

时间:2015-06-02 19:43:58

标签: r plot

我正在将几个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))

然而,我收到错误:

Error in arrangeGrob(..., as.table = as.table, clip = clip, main = main,  : 
  input must be 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)

收到错误:

Error in mget(c(plot_1[[1]], plot_1[[2]], plot_2)) : 
  invalid first argument

2 个答案:

答案 0 :(得分:1)

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

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

自: p = do.call(&#34; grid.arrange&#34;,c(plot_1 [[1]],plot_1 [[2]],plot_2,ncol = 1))

于: p = do.call(&#34; grid.arrange&#34;,c(list(plot_1 [[1]],plot_1 [[2]],plot_2),ncol = 1))

我相信这会解决问题。

答案 1 :(得分:0)

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#