I wrote a function that will plot a user-specified number of parallel coordinate subplots, all in one big plot with one column:
library(gridExtra)
library(GGally)
plotClusterPar = function(cNum){
plot_i = vector("list", length=cNum)
for (i in 1:cNum){
x = data.frame(a=runif(100,0,1),b=runif(100,0,1),c=runif(100,0,1),d=runif(100,0,1))
plot_i[[i]] = ggparcoord(x, columns=1:4, scale="globalminmax", alphaLines = 0.9)+ylab("Count")
}
p = do.call("grid.arrange", c(plot_i, ncol=1))
}
The user will call (to create 3 subplots):
plotClusterPar(3)
There are four things I am trying to do, and when I try them, I get errors, so I left it at its bare working syntax! Here is what I aim to do:
1) I desire to have one y-axis label "Count", rather than an individual one for each subplot.
2) I do not wish to have any x-axis label. As default (currently), there is the label "variable" indicated under each subplot. If extra space (in between subplots) is created after removing an x-axis label, then I would like to erase that newly-created horizontal space (in between subplots).
3) I hope to color all lines in each subplot the same color. For instance, the top subplot would have all red lines, the next subplot would have all blue lines, etc. I do not mind what the colors are!
4) I strive to have a color legend at the bottom of all the subplots. (Similar to the first answer here: Universal x axis label and legend at bottom using grid.arrange), but of course the number of colors is just equal to the number of subplots.
EDIT:
I have tried to change the color, using things like:
plot_i[[i]] = ggparcoord(x, columns=1:4, scale="globalminmax", alphaLines = 0.9, colour=i)+ylab("Count")
Or hardcoding, which would not even work, because I have a loop. But this still does not work:
plot_i[[i]] = ggparcoord(x, columns=1:4, scale="globalminmax", alphaLines = 0.9, colour="red")+ylab("Count")
I tried adding colour as a layer, but that does not work:
plot_i[[i]] = ggparcoord(x, columns=1:4, scale="globalminmax", alphaLines = 0.9)+ylab("Count")+colour("red")
I also tried to give a common plot title and y-axis:
plotClusterPar = function(cNum){
plot_i = vector("list", length=cNum)
for (i in 1:cNum){
x = data.frame(a=runif(100,0,1),b=runif(100,0,1),c=runif(100,0,1),d=runif(100,0,1))
plot_i[[i]] = ggparcoord(x, columns=1:4, scale="globalminmax", alphaLines = 0.9)
}
p = do.call("grid.arrange", c(plot_i, ncol=1, main = textGrob("Main Title", vjust = 1, gp = gpar(fontface = "bold", cex = 1.5)), left = textGrob("Global Y-axis Label", rot = 90, vjust = 1)))
}
But this led to an error:
Error in arrangeGrob(..., as.table = as.table, clip = clip, main = main, :
input must be grobs!