Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
597 views
in Technique[技术] by (71.8m points)

r - How to save output from ggforce::facet_grid_paginate in only one pdf?

I'm using the ggforce package to generate facetted plots over several pages:

library(ggforce) 

for(i in 1:6){
  ggplot(diamonds) +
    geom_point(aes(carat, price), alpha = 0.1) +
    facet_wrap_paginate(~cut:clarity, ncol = 2, nrow = 2, page = i)

  ggsave(paste0("~/diamonds_", i, ".pdf"))
}

which is generating the expected 6 PDF files:
enter image description here

What is the easiest way have the output in one single pdf with 6 pages?

I understand this can be done with the reports and pdftools packages, but I'm wondering if there's a more direct way to accomplish this. I'd expect ggforce to provide the functionality for the output to be single-paged, but it looks like that's not the case?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You don't even need to use ggsave you can put all these plots into one pdf by:

pdf("~/diamonds_all.pdf")
for(i in 1:6){
  print(ggplot(diamonds) +
          geom_point(aes(carat, price), alpha = 0.1) +
          facet_wrap_paginate(~cut:clarity, ncol = 2, nrow = 2, page = i))

}
dev.off()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...