r - Using grid.arrange with multiple plots -
i plotting several ggparcoord (from ggally package) subplots 1 large plot. in general, 1 of subplots derive same data set (x), , last 1 comes different data set (y).
i want each subplot different color. strangely, can work when not in loop follows (in case have 3 subplots):
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))
however, trying automate subplots come same data set (x), , running difficulties. in example above, 2 subplots. increasing number. in case, however, last subplot other data set (y). reason, trying create loop go through many subplots of data set (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))
however, error:
error in arrangegrob(..., as.table = as.table, clip = clip, main = main, : input must grobs!
i tried similar ideas (grid.arrange using list of plots):
plist <- mget(c(plot_1[[1]], plot_1[[2]], plot_2)) do.call(grid.arrange, plist, ncol = 1)
and received error:
error in mget(c(plot_1[[1]], plot_1[[2]], plot_2)) : invalid first argument
the thing missing when inputting multiple plots, need in list structure.
if change last line of code
from: p = do.call("grid.arrange", c(plot_1[[1]], plot_1[[2]], plot_2, ncol=1))
to: p = do.call("grid.arrange", c(list(plot_1[[1]], plot_1[[2]], plot_2), ncol=1))
i believe solve issue.
Comments
Post a Comment