将 grid.arrange 用于多个绘图
Using grid.arrange with multiple plots
我正在将几个 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
唯一缺少的是,当您输入多个图时,它们需要采用列表结构。
如果您更改最后一行代码
来自:
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))
我相信这会解决问题。
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#
我正在将几个 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
唯一缺少的是,当您输入多个图时,它们需要采用列表结构。
如果您更改最后一行代码
来自:
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))
我相信这会解决问题。
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#