Grid of multiple ggplot2 plots which have been made in a for loop

后端 未结 2 566
难免孤独
难免孤独 2020-11-29 23:57

as a new ggplot2 user, I am a bit lost with the amount of possibilities, and struggle to find on the net a simple answer to what I consider a simple problem.

I would

2条回答
  •  眼角桃花
    2020-11-30 00:44

    This is my solution. Tiny change in the ggplot function with the mapping parameter to aes_string.

    library(gridExtra)
    library(ggplot2)
    p <- list()
    for(i in 1:4){
    p[[i]] <- ggplot(data=df,aes_string(x=df$x,y=df$y) +geom_bar(aes_string(x=class.names[i],fill=var.names[j])
    }
    do.call(grid.arrange,p)
    

    Hope this helps!

提交回复
热议问题