在R生成的pdf文件中添加页码(Adding page numbers in pdf file generated by R)

我正在尝试将页码添加到使用R中的绘图生成的pdf文件中并以pdf格式保存。 我正在使用d_pply到我正在使用plot命令的data.frame 。

我以为d_pply可以帮助我避免for循环。 以下是我原始数据中的样本以及更多因素。

data1 <- structure(list(fact = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L ), .Label = c("A", "B", "C"), class = "factor"), speed = c(10.56, 11.94, 13.61, 15, 16.67, 18.06, 19.44, 20.28, 21.11, 21.67, 22.5, 23.06, 23.61, 24.44, 25, 25.56, 26.11, 26.94, 27.5, 15.83, 16.67, 17.5, 18.06, 18.89, 19.72, 20.56, 21.11, 21.94, 22.5, 23.33, 23.89, 24.44, 25, 25.56, 26.11, 26.67, 27.22, 8.61, 10.28, 11.94, 13.61, 15, 16.39, 17.5, 18.89, 19.72, 20.83, 21.67, 22.22, 22.5, 23.06, 23.61, 23.89, 23.89, 23.61)), .Names = c("fact", "speed" ), class = "data.frame", row.names = c(NA, -55L))

我试图通过使用全局索引来完成任务 。 但我正在寻找一种有效的方法来做到这一点。 这个对我没有帮助。

index1 <<- 0 plot_pg <- function(x) { index1 <<- index1+1 plot(x$speed,main=paste0('pg# ',index1)) } genplot <- function(df1,filename1) { pdfNAME <- paste0(name1,'.pdf') pdf(pdfNAME) d_ply(df1,c('fact'),function(x) plot_pg(x)) dev.off() } genplot(data1,'data1Plots')

更新

我应该在这里提一下,我将把我的data.frame拆分为多个变量ddply(data,c('var1','var2'),function(x) MyplotFunc(x))像ddply(data,c('var1','var2'),function(x) MyplotFunc(x))类的ddply(data,c('var1','var2'),function(x) MyplotFunc(x))

I am trying to add page numbers to a pdf file generated using plot in R and saved in a pdf format. I am using d_pply to the data.frame within which I am using the plot command.

I thought d_pply would help me in avoiding a for loop. Below is the sample from my original data with many more factors.

data1 <- structure(list(fact = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L ), .Label = c("A", "B", "C"), class = "factor"), speed = c(10.56, 11.94, 13.61, 15, 16.67, 18.06, 19.44, 20.28, 21.11, 21.67, 22.5, 23.06, 23.61, 24.44, 25, 25.56, 26.11, 26.94, 27.5, 15.83, 16.67, 17.5, 18.06, 18.89, 19.72, 20.56, 21.11, 21.94, 22.5, 23.33, 23.89, 24.44, 25, 25.56, 26.11, 26.67, 27.22, 8.61, 10.28, 11.94, 13.61, 15, 16.39, 17.5, 18.89, 19.72, 20.83, 21.67, 22.22, 22.5, 23.06, 23.61, 23.89, 23.89, 23.61)), .Names = c("fact", "speed" ), class = "data.frame", row.names = c(NA, -55L))

I tried to accomplish the task by using a global index. But I am looking for an efficient way to do this. This one did not quite help me.

index1 <<- 0 plot_pg <- function(x) { index1 <<- index1+1 plot(x$speed,main=paste0('pg# ',index1)) } genplot <- function(df1,filename1) { pdfNAME <- paste0(name1,'.pdf') pdf(pdfNAME) d_ply(df1,c('fact'),function(x) plot_pg(x)) dev.off() } genplot(data1,'data1Plots')

Update

I should mention here that I would be splitting my data.frame by more than one variables..something like ddply(data,c('var1','var2'),function(x) MyplotFunc(x))

最满意答案

我会这样做:

genplot <- function(df1,filename1){ pdfNAME <- paste0(filename1,'.pdf') tmp <- split(df1,df1$fact) pdf(pdfNAME) for (i in seq_along(tmp)){ plot(tmp[[i]][,'speed'],main = paste0("pg#",i)) } dev.off() }

for循环本身就很慢的想法是一个神话。 问题是,在for循环中很容易陷入错误的编码技术,这使得你正在进行的操作需要很长时间。

在这种情况下,你在for循环中所做的一切都是绘图,所以我怀疑这与使用像lapply这样的东西之间会有很大的性能差异。 需要注意的是增长对象(即附加)和修改对象,因为两者都会导致过多的复制。

I would simply do this:

genplot <- function(df1,filename1){ pdfNAME <- paste0(filename1,'.pdf') tmp <- split(df1,df1$fact) pdf(pdfNAME) for (i in seq_along(tmp)){ plot(tmp[[i]][,'speed'],main = paste0("pg#",i)) } dev.off() }

The idea that for loops are inherently slow is a myth. The issue is that it can be easy to slip into bad coding techniques inside the for loop that makes the operations you're doing take a long time.

In this case, all you're doing in the for loop is plotting, so I doubt there will be much of a performance difference between this and using something like lapply. The things to watch out for are growing objects (i.e. appending) and modifying objects, since both will result in excessive copying.

更多推荐