show multiple plots from ggplot on one page in r

后端 未结 2 1190
灰色年华
灰色年华 2020-12-07 02:18

I want to make multiple ggplot in a loop and show them on one plot.

for ( i in 1:8) {
    g <- ggplot(data=mtcars, aes(x=hp, y=wt))+
        geom_point()
         


        
2条回答
  •  广开言路
    2020-12-07 03:10

    Note that, in the loop you provided, the counter i isn't referenced in the plot, so you'll end up printing the same plot eight times!

    If you have a bunch of different subsets of a single dataset and want to lay them out, you can follow @PKumar's comment and check out facetting in ggplot2. It essentially splits your data up into groups according to one or more of your columns and then lays them out in a grid or a ribbon.

    On the other hand, if you have a bunch of a different plots that you want to combine on one page, there're a couple of packages that can make this happen:

    • cowplot is a fairly mature package that can do this, and
    • patchwork is a newer package that lets you lay plots out using arithmetic.

    Hope those help!

提交回复
热议问题