2 graphs in the same plot

#1
HI All, I have the following 2 graphs:
Code:
mat <- matrix(c(107.6937,75.23753,61.22222,39.83478,23.69258,14.5623,6.201099,3.622861,2.132939,
141.0136,103.0853,82.92933,54.3715,33.37596,20.16385,8.279188,4.765267,2.781721,
165.4013,127.2847,100.6411,65.60381,40.81477,24.12292,9.4888,5.35654,3.187534,
226.4852,203.4411,151.849,95.81119,60.66724,33.59892,11.75319,6.31541,4.01861,
255.7309,248.4244,179.865,111.2717,70.75567,37.94386,12.56757,6.612455,4.345836), ncol=5, nrow=9,
 dimnames=list(time=c("5min","10min" , "15min","30min","1H","2H","6H","12H","24H"), 
year=c("2years", "5years","10years","50years","100years")))

 mat2=as.data.frame(as.table(mat))

require("reshape")
require("ggplot2")

X=ggplot(data=mat2 ,
       aes(x=time, y= Freq, colour= year,group=year)) +
    geom_line()
and the other graph is:
Code:
mat <- matrix(c(109.8392,
79.75906,64.01432,41.20325,24.51792,14.77412,
5.978663,
3.416812,
2.117014,
143.959,
108.83485,
86.93392,
56.42561,
35.14413,
20.6033,
7.974953,
4.511107,
2.75926,
166.6024,
128.13091,
102.14444,
66.52787,
42.19619,
24.47181,
9.299784,
5.237332,
3.185484,
216.333,
170.50978,
135.55046,
88.71493,
57.68423,
32.96802,
12.209439,
6.832301,
4.121577,
237.3922,
188.45577,
149.69676,
98.11038,
64.24288,
36.56586,
13.441577,
7.507715,
4.51798

), ncol=5, nrow=9,
 dimnames=list(time=c("5min","10min" , "15min","30min","1H","2H","6H","12H","24H"), 
year=c("2years", "5years","10years","50years","100years")))

 mat22=as.data.frame(as.table(mat))




require("reshape")
require("ggplot2")

Y=ggplot(data=mat2 ,
       aes(x=time, y= Freq, colour= year,group=year)) +
    geom_line()

Now my question is how to plot the 2 graphs on the same plot
 

trinker

ggplot2orBust
#3
I don't think layout will work for grid. Try the gridExtra package. If you want to actually put one plot on another you may want to use Viewports.
 

bugman

Super Moderator
#4
Do you mean in the same plotting region?

If so, try the multiplot function:

Code:
multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
  require(grid)
  
  # Make a list from the ... arguments and plotlist
  plots <- c(list(...), plotlist)
  
  numPlots = length(plots)
  
  # If layout is NULL, then use 'cols' to determine layout
  if (is.null(layout)) {
    # Make the panel
    # ncol: Number of columns of plots
    # nrow: Number of rows needed, calculated from # of cols
    layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
                     ncol = cols, nrow = ceiling(numPlots/cols))
  }
  
  if (numPlots==1) {
    print(plots[[1]])
    
  } else {
    # Set up the page
    grid.newpage()
    pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))
    
    # Make each plot, in the correct location
    for (i in 1:numPlots) {
      # Get the i,j matrix positions of the regions that contain this subplot
      matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))
      
      print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
                                      layout.pos.col = matchidx$col))
    }
  }
}

multiplot(X,Y)
to get:
 
Last edited: