我正在将几个ggparcoord(来自GGally package)子图绘制成一个大图。一般来说,除了一个子图外,所有子图都来自同一数据集(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))
但是,我得到一个错误:
安排grob(…)错误,如。表= as。表,clip = clip, main = main,:输入必须是grobs!
我尝试了类似的想法(网格)。使用地块列表排列):
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)):第一个参数无效
唯一缺少的是,当您输入多个情节时,它们需要在列表结构中。
如果您更改了最后一行代码
来自: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#