Plot over multiple pages

后端 未结 3 825
北荒
北荒 2020-11-27 19:07

I have the facet_wrap function to make multiple graphs (n=~51) but they all appear on one page. Now after searching, I found out that ggplot2 can\'t pl

3条回答
  •  心在旅途
    2020-11-27 19:53

    There are multiple ways to do the pagination: ggforce or gridExtra::marrangeGrob. See also this answer for another example.

    ggforce:

    library(ggplot2)
    # install.packages("ggforce")
    library(ggforce)
    
    # Standard facetting: too many small plots
    ggplot(diamonds) +
      geom_point(aes(carat, price), alpha = 0.1) +
      facet_wrap(~cut:clarity, ncol = 3)
    

    # Pagination: page 1
    ggplot(diamonds) +
      geom_point(aes(carat, price), alpha = 0.1) +
      facet_wrap_paginate(~cut:clarity, ncol = 3, nrow = 3, page = 1)
    

    # Pagination: page 2
    ggplot(diamonds) +
      geom_point(aes(carat, price), alpha = 0.1) +
      facet_wrap_paginate(~cut:clarity, ncol = 3, nrow = 3, page = 2)
    

    # Works with grid as well
    ggplot(diamonds) +
      geom_point(aes(carat, price), alpha = 0.1) +
      facet_grid_paginate(color~cut:clarity, ncol = 3, nrow = 3, page = 4)
    

    gridExtra:

    # install.packages("gridExtra")
    library(gridExtra)
    
    set.seed(123)
    pl <- lapply(1:11, function(.x) 
      qplot(1:10, rnorm(10), main=paste("plot", .x)))
    
    ml <- marrangeGrob(pl, nrow=2, ncol=2)
    
    ## non-interactive use, multipage pdf
    ## ggsave("multipage.pdf", ml)
    ## interactive use; calling `dev.new` multiple times
    ml
    

    Created on 2018-08-09 by the reprex package (v0.2.0.9000).

提交回复
热议问题