Our
Our

Reputation: 1035

ggplot: stacking multiple plots into the same figure and scaling them appropriately

I've a set of simulations, and I would like to plot certain graphs for each of those simulations using ggplot2 and put them into the same figure, as a single row.

I've googled it a lot but it seems that every page gives the same old example and none of them clearly explains how to do it.

Here is what I want to do pseudo-code:

figure <- newFigure(nrows=dim(filelist)[1])
for(file in filelist)
{
d<- getData()
graph1 <- ggplot2(...)
figure <- rbind(figure, graph1)
graph2 <- ggplot2(...)
figure <- rbind(figure, graph2)
}

scale(figure, way="fit-appropriately")
draw(figure)

Upvotes: 1

Views: 288

Answers (1)

Waldi
Waldi

Reputation: 41220

You could create an initial ggplot() object and add geometries to it.
Try:

library(ggplot2)

filelist <- list('red','blue','green')
p <- ggplot()
for (file in filelist)  {
  mydata <- data.frame(x=1:10,y=sample(1:10,10,replace=T))
  p <- p + geom_line(data=mydata,aes(x,y),color=file)
}
p <- p + coord_cartesian(ylim = c(-5,15))
p

enter image description here

Upvotes: 2

Related Questions