0

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

NelsonGon
  • 13,015
  • 7
  • 27
  • 57

2 Answers2

2

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.

NelsonGon
  • 13,015
  • 7
  • 27
  • 57
easports611
  • 442
  • 3
  • 12
  • That works like a charm. I wonder if I could ask here: Is it possible to call this without hard-coding in the plot_1[[1]] and plot_1[[2]]? The reason is, I am not always going to have exactly two subplots in plot_1 (although I will always have one subplot in plot_2). –  Jun 03 '15 at 14:03
  • For instance, I would define a variable nPlots. In the example above, nPlots = 2. I tried doing things that would not explicitly hardcode in the number 2, such as: 1) p = do.call("grid.arrange", c(list(plot_clusters, plot_filtered), ncol=1)), 2) p = do.call("grid.arrange", c(list(paste0("plot_clusters[[", 1:nPlots, "]]", sep=","), plot_filtered), ncol=1)). –  Jun 03 '15 at 14:07
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#
easports611
  • 442
  • 3
  • 12
  • i wrote #new code# where there is a change. Note * I commented out where you initialize plot_1 and plot_2. This is not necessary because you overwrite plot_1 and plot_2 later in the code. Leaving it in the code, however, displays your intention for those objects, which is never a bad thing. – easports611 Jun 04 '15 at 01:06