【问题标题】:how to print loop plotting into single pdf /multiple pdf files [duplicate]如何将循环绘图打印到单个pdf /多个pdf文件中[重复]
【发布时间】:2021-01-08 00:09:06
【问题描述】:

我正在做循环绘图,并且想将绘图打印成 pdf。我该怎么办。我很困惑。

循环代码如下所示:

for (i in 1:12)){

   df<-Sample.Data %>% filter(ID==i) 
         p1 <- ggplot(data =df)+
   geom_line(aes(x = Days, y = Result, color = Item))+
      
 print(p1)
 }

那么,我该怎么办?

【问题讨论】:

    标签: r pdf


    【解决方案1】:

    pdf函数试试这个(没有提供数据,所以不可能添加一些输出):

    #Pdf
    pdf('Example.pdf',width = 14)
    #Loop
    for (i in 1:12)){
      #Data
      df<-Sample.Data %>% filter(ID==i) 
      #Plot
      p1 <- ggplot(data =df)+
        geom_line(aes(x = Days, y = Result, color = Item))
      #Print
      plot(p1)
    }
    dev.off()
    

    【讨论】:

    • 我可以定义输出位置吗?如果我将我的位置定义为outfile,那么如何在该位置输出名为“test.pdf”的pdf?
    • @Stataq 您必须使用setwd() 设置一个目录,然后您的pdf将保存在该位置。
    • 我没有使用setwd(),而是使用out_dir = "//Statistics"
    • @Stataq 在这种情况下试试这个:pdf(paste0(out_dir,'//Example.pdf'),width = 14) 在代码的开头!
    • 非常感谢!它有效。
    猜你喜欢
    • 2013-07-01
    • 2010-11-26
    • 2018-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-26
    相关资源
    最近更新 更多