[英]R Generate Multiple Plots for a single pdf
I have a list of about 100 different groups; 我列出了大约100个不同的组。
categories <- c('cat1','cat2','cat3',....)
I would like to write a loop that goes and grabs only the data for 1 category at a time, and generate a plot (x,y); 我想编写一个循环,一次仅获取1个类别的数据,并生成图(x,y); for example... 例如...
for (i in 1:length(categories){
category_data <- data[which(data$category==categories[i]),]
plot(category_data$y,category_data$x)}
### however, I would like to save EACH plot to a single PDF that I can scroll through to quickly visualize everything at once
How can I generate a single PDF with 100 different plots? 如何生成包含100个不同图的单个PDF?
You can generate a multiple page pdf by using the pdf function (or png, jpeg, etc), generating all of the files that you want to and then closing the file with dev.off 您可以通过使用pdf函数(或png,jpeg等)生成多页pdf,生成所需的所有文件,然后使用dev.off关闭文件
pdf(file = "output.pdf")
for (i in 1:length(categories){
category_data <- data[which(data$category==categories[i]),]
plot(category_data$y,category_data$x)}
dev.off()
I used R for much of the graphing in my college statistics and calculus classes. 在大学统计和微积分课程中,大部分图形使用R进行。 The best way I found to incorporating multiple plots into a single PDF was to apply the principle of Separation of Concerns . 我发现将多个图合并到单个PDF中的最佳方法是应用关注点分离原则。
R was great at generating the plots (I favored the png format), but LaTeX rocked for generating PDFs. R擅长生成绘图(我更喜欢png格式),但LaTeX摇晃以生成PDF。 My simplified workflow looked something like this: 我简化的工作流程如下所示:
graph_01.png
, etc). 在已知文件夹中生成绘图,并使用顺序生成的名称( graph_01.png
等)。 pdflatex
使用pdflatex
生成pdf It was very easy, and almost completely automated. 这非常简单,几乎完全自动化。
Based on the OP wanting 4 plots per PDF page, I would recommend: 基于希望每个PDF页面有4个图的OP,我建议:
pdf('plots.pdf')
par(mfrow=c(2,2))
for (i in 1:length(categories){
category_data <- data[which(data$category==categories[i]),]
plot(category_data$y,category_data$x)}
dev.off()
If the OP decides to plot a different number of graphs per page, the mfrow
parameter can be changed. 如果OP决定每页绘制不同数量的图形,则可以更改mfrow
参数。 Currently it is set for 2 rows x 2 columns = 4 plots per page. 当前设置为2行x 2列=每页4个图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.