4

我对 facet_wrap_paginate 函数有疑问:我有几组图表。每组中图表的数量各不相同。当我使用 facet_wrap_paginate 将 5x4 图表中的每个组绘制到一个 pdf 上时,如果最后一个 pdf 页面的图太少,我会收到错误消息,因此第一行图没有“填充”。

例如,一组地块包含 22 个地块,因此第一页有 20 个地块,而第二页只有 2 个地块,这会导致错误:

错误:无法创建零长度单位向量(“单位”子集)

这是示例代码:

for (i in Classes)
{
  x <- filter(all_data_R2_filtered, class.x== "i" )
  
  bp <- ggplot(data = x, aes(x=dil, y=mmol, color=AcquiMeth))+
    geom_point()+
    facet_wrap_paginate(name.x ~ ., ncol = 5, nrow = 4, scales = "free")
    number_of_pages <- n_pages(bp)
  
  pdf(paste("class",i,".pdf", sep = ""), paper= 'A4r', w= 297, h= 210)
  
  for(j in 1:number_of_pages){
    print(bp + facet_wrap_paginate(name.x ~ . , ncol = 5,  nrow = 4, page = j))
    }
  dev.off()
}

有解决办法吗?非常感谢您的帮助

4

0 回答 0