0

every one.
I have a function g(mua=1) which will plot a ggplot2 figure like this ( to illustrate the concept of POWER):
enter image description here

But I want plot 5 figures with different parameter. Like this

for(mua in c(0, .5, 1, 1.5, 2))
{     
      g(mua)
}

And I wish the figure is like this enter image description here
So, is that possible? because I know in base system, I could call par(mfrow=c(2,3)).

here is all the code I have now

g <- function(mua=1.2){
mu0=0


ggplot(data.frame(x=c(-4.5, 4.5)), 
       aes(x)
) + 
    stat_function(fun=dnorm,args = list(mean=mu0), size = 1.5) + 
    stat_function(fun=dnorm,args = list(mean=mua), size = 1.5) + 
    geom_area(stat = "function", fun = dnorm,args=list(mean=mua), fill = "red", 
              xlim = c(qnorm(1-0.025,mean = mu0), 5), 
              alpha = 0.4) +
    annotate(geom = "text",x=mu0,y=0,label="mu0") +
    annotate(geom = "text",x=mua,y=0,label="mua") +
    annotate(geom = "text",x=qnorm(1-0.025,mean = mu0),
             y=0,label=round(qnorm(1-0.025,mean = mu0),2))
}

# something like "par(mfrow=c(4,1))"

for(i in seq(0,2,by=0.5)){
    g(i)
}
Matthew May
  • 113
  • 1
  • 1
  • 10
  • Seems you just want to arrange *grobs*, see [here](https://stackoverflow.com/questions/35068129/arrange-ggplot-plots-grobs-with-same-widths-using-gtable-to-create-2x2-layout), this is essentially a duplicate – Kevin Arseneau Jan 21 '18 at 02:10
  • 3
    [patchwork](https://github.com/thomasp85/patchwork) is a nice way to arrange grobs. – alistaire Jan 21 '18 at 02:20
  • @alistaire patchwork is so cool that I had to thank you in way that is more personal than an up-vote. Thank you! ~missuse. – missuse Jan 21 '18 at 09:44
  • @KevinArseneau Thank you! I write the code according to the link you give and post it as answers. – Matthew May Jan 23 '18 at 05:15
  • @alistaire Patchwork looks nice and easy to use. Thank you! – Matthew May Jan 23 '18 at 05:29

1 Answers1

0

Thank you,@Kevin Arseneau. I write the code according to the link you give.

library(ggplot2)
g <- function(mua=1.2){
  mu0=0

  g1<-ggplot(data.frame(x=c(-4.5, 4.5)), 
         aes(x)
  ) + 
    stat_function(fun=dnorm,args = list(mean=mu0), size = 1) + 
    stat_function(fun=dnorm,args = list(mean=mua), size = 1) + 
    geom_area(stat = "function", fun = dnorm,args=list(mean=mua), fill = "red", 
              xlim = c(qnorm(1-0.025,mean = mu0), 5), 
              alpha = 0.4) +
    annotate(geom = "text",x=mu0,y=0,label="mu0") +
    annotate(geom = "text",x=mua,y=0,label="mua") +
    annotate(geom = "text",x=qnorm(1-0.025,mean = mu0),
             y=0,label=round(qnorm(1-0.025,mean = mu0),2))
  g1
}

plots1 <- vector("list", 6)
index=1
for(i in seq(0,2.5,by=0.5)){
  plots1[[index]]=g(i)
  index=index+1
}
plots1[[6]]=ggplot()

library(grid)
grobs1 = lapply(plots1, ggplotGrob)

row1 = do.call(cbind, c(grobs1[1:3], size="first"))
row2 = do.call(cbind, c(grobs1[4:6], size="first"))
grid1 = do.call(rbind,c(list(row1,row2), size="first"))
grid.newpage()
grid.draw(grid1)

enter image description here

Matthew May
  • 113
  • 1
  • 1
  • 10