user4148072
user4148072

Reputation:

Using grid.arrange with multiple plots

I am plotting several ggparcoord (from GGally package) subplots into one large plot. In general, all but one of the subplots derive from the same data set (x), and the last one comes from a different data set (y).

I want each subplot to be a different color. Strangely, I can get this to work when I do it not in a for loop as follows (in this case I 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, I am trying to automate all the subplots that come from the same data set (x), and running into difficulties. In the example above, this was only 2 subplots. But I will be increasing this number. In any case, however, the last subplot will always be from the other data set (y). For this reason, I am trying to create a loop to go through the many subplots of the 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, I get an error:

Error in arrangeGrob(..., as.table = as.table, clip = clip, main = main, : input must be grobs!

I tried similar ideas to (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 an error:

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

Upvotes: 0

Views: 1095

Answers (2)

easports611
easports611

Reputation: 482

The only thing missing is that when you are inputting multiple plots, they need to be in a list structure.

If you change your 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 that will solve the issue.

Upvotes: 2

easports611
easports611

Reputation: 482

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#

Upvotes: 0

Related Questions