【问题标题】:How to create a function with a for loop to obtain multiple pdf files (unique per id) with ggplots on spearate pages如何使用 for 循环创建函数以在单独的页面上使用 ggplots 获取多个 pdf 文件(每个 id 唯一)
【发布时间】:2020-12-11 20:44:09
【问题描述】:

我正在尝试使用函数和 for 循环创建 pdf 报告文件,但我希望文件中每页有 1 个 ggplot。我创建了这个函数和 for 循环,以便能够为每个名称生成 1 个报告,因为它需要个性化。到目前为止,我的代码看起来像这样

tv<- data.frame(
  name = c("p1","p2","p3","p1","p2","p3","p1","p2","p3","p1","p2","p3", "p1", "p2", "p3", "p1", "p2", "p3", "p1", "p2", "p3", "p1", "p2", "p3", "p1", "p2", "p3"),
  dates = c("2010", "2010", "2010", "2010", "2010", "2010", "2010", "2010", "2010","2011", "2011", "2011", "2011", "2011", "2011", "2011", "2011", "2011", "2015", "2015", "2015", "2015", "2015", "2015", "2015", "2015", "2015" ),
  results = c(40, 40, 45, 50, 52, 52, 53, 54, 56, 70, 50, 10, 40, 55, 55, 60, 60, 70, 30, 60, 60, 55, 55, 54, 32, 33, 57),
  parameter = c("D", "D", "D", "R", "R", "R", "C", "C", "C", "D", "D", "D", "R", "R", "R", "C", "C", "C","D", "D", "D", "R", "R", "R", "C", "C", "C")
)


ftv <- function(id){
  Ttv <- tv %>% filter(name == id)
  ggplot(Ttv, aes(dates, results, group = parameter)) +
    geom_point() +
    geom_line() +
    theme(axis.text.x = element_text(size= 6, angle = 90), strip.text = element_text(size = 6))+
    facet_wrap(~parameter, scales = "free")+
  ggsave(paste0("~/Desktop//", id, "_test.pdf"))
}

for(id in unique(tv$name)){
  ftv(id)
}


我得到了这个

我想拥有相同类型的文件,但每页包含 1 个绘图,考虑到稍后我可能希望文件中包含 3 个以上的图形。

谢谢你的帮助。

【问题讨论】:

  • 所以你希望每个方面都在一个不同的页面中,或者所有的图都在不同的页面中而不考虑 id?
  • @Duck 我希望每个方面都在一个不同的页面中。每个 id 都应该创建自己的 pdf 文件

标签: r c for-loop ggplot2


【解决方案1】:

试试这个:

#Function
ftv <- function(id){
  Ttv <- tv %>% filter(name == id)
  LTtv <- split(Ttv,Ttv$parameter)
  Lplot <- lapply(LTtv, function(x) 
    ggplot(x, aes(dates, results, group = parameter)) +
    geom_point() +
    geom_line() +
    theme(axis.text.x = element_text(size= 6, angle = 90), strip.text = element_text(size = 6))+
    facet_wrap(~parameter, scales = "free"))
  #Export
  pdf(paste0(id,'.pdf'),width = 14)
  lapply(Lplot,plot)
  dev.off()
}
#Apply
for(id in unique(tv$name)){
  ftv(id)
}

【讨论】:

    【解决方案2】:

    这是你想要的吗?

    # create a new variable that combines the two variables for identifying cases    
    tv$name_parameter<-paste0(tv$name,tv$parameter)
    
    
    ftv <- function(id){
    
    Ttv <- tv %>% filter(name_parameter == id) # use the new identifier here
    ggplot(Ttv, aes(dates, results, group = parameter)) +
    geom_point() +
    geom_line() +
    theme(axis.text.x = element_text(size= 6, angle = 90), strip.text = element_text(size = 6))+
    # # remove facet_wrap bc you're using `parameter` to paginate   
    # facet_wrap(~parameter, scales = "free")+   
    ggsave(paste0("~/Desktop//", id, "_test.pdf"))
     }
    
    
    for(id in unique(tv$name_parameter)){  # again, the new identifier
    ftv(id)
    }
    

    【讨论】:

    • 不完全是,我希望能够创建 1 个 pdf 文件名,例如,“p1”作为 1 个 pdf 文件,3 页,每个参数 1 个,“p2”和“p3”
    猜你喜欢
    • 1970-01-01
    • 2020-09-29
    • 1970-01-01
    • 2021-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多