簡體   English   中英

帶有循環的pdf上的R中的多個圖

[英]Multiple plots in R on a pdf with a loop

我想在循環方面需要幫助。 您將如何使用以下數據在單獨的pdf頁面上進行多個繪圖:

pdf頁面1:mpg vs cyl和mpg vs vs的馬自達RX4 2面板圖

pdf第2頁:大黃蜂4 D 2 mpg vs cyl和mpg vs vs面板圖

和Valiant一樣。

型號mpg cyl vs馬自達RX4 21.0 6 0馬自達RX4 22.8 4 1大黃蜂4 D 21.4 6 1大黃蜂4 D 18.7 8 0英勇18.1 6 1英勇21.4 6 1英勇21.0 6 0英勇22.8 6 0

謝謝。

在這種情況下,我要做的是使用gridExtra在一頁上設置所需的圖,將其另存為PDF,然后將所有這些PDF與ghostscript連接起來。

在R中:

library(gridExtra)
library(ggplot2)

plot_one <- ggplot() + geom_...
plot_two <- ggplot() + geom_...

# Arrange the two plots one per row.
# grid.arrange'd plots can be nested, too!
two_rows <- grid.arrange(plot_one, plot_two, nrow = 2)

ggsave("dataset_1.pdf", two_rows)

# repeat for second, third, etc datasets so you end up with dataset_2.pdf etc

然后將它們連接到一個包含多頁帶有ghostscript的PDF:

gs -sDEVICE=pdfwrite \
   -dNOPAUSE \
   -dQUIET \
   -dBATCH \
   -sOutputFile=multipage.pdf \
   dataset_1.pdf dataset_2.pdf

源自其他地方的示例( https://www.researchgate.net/post/How_to_save_the_graphics_in_several_separate_pages_with_R

# Create pdf
pdf(...)

# Create different plots
plot1(...)
plot2(...)
plot3(...)

dev.off()

注意:在pdf()設置參數onefile=FALSE

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM