{"id":10929,"date":"2021-03-28T19:24:00","date_gmt":"2021-03-28T11:24:00","guid":{"rendered":"https:\/\/ukyoi.info\/wordpress\/?p=10929"},"modified":"2021-03-28T19:26:39","modified_gmt":"2021-03-28T11:26:39","slug":"prevent-foremost-blank-page-when-plotting-to-pdf-in-r-with-survminer","status":"publish","type":"post","link":"https:\/\/ukyoi.info\/wordpress\/?p=10929","title":{"rendered":"Prevent foremost blank page when plotting to PDF in R (with survminer)"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">When I was generating a a series of plots using package &#8220;survminer&#8221; in R, I often suffer from a blank page generated automatically at foremost of the PDF. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>library(survival)\nlibrary(survminer)\n\n# Using lung dataset.\n\ncairo_pdf(filename = \".\/temp_plot.pdf\", onefile = TRUE)\nfor (single_sex in unique(lung$sex)) {\n    lung_single_sex = subset(lung, sex == single_sex)\n    cox_result = coxph(Surv(time, status) ~ age + ph.ecog, data = lung_single_sex)\n    print(ggforest(cox_result, data = lung_single_sex))\n}\ndev.off()\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then there will be three pages in <code>temp_plot.pdf<\/code> and the first page will be blank.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I don&#8217;t know the exact reason of the issue. But it seems <code>ggforest()<\/code> called <code>ggpubr::as_ggplot()<\/code>, which called  <code>gridExtra:::grid.newpage()<\/code> or something similar, then caused the blank page before the plot. I searched the issue on the web and most people advised setting <code>onefile = FALSE<\/code> in <code>cairo_pdf()<\/code> to prevent the blank page. While it is not an ideal workaround because it makes the PDF contain only the last plot written to the file.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">After some attempts, I found a relatively better workaround. The key is the plot(s) must be generated <strong>BEFORE<\/strong> (but not after) the PDF device opened. We can use a list to store the plot(s) which will be plotted on the PDF. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>p_list = list() # To store the plots.\nfor (single_sex in unique(lung$sex)) {\n    lung_single_sex = subset(lung, sex == single_sex)\n    cox_result = coxph(Surv(time, status) ~ age + ph.ecog, data = lung_single_sex)\n    p = ggforest(cox_result, data = lung_single_sex)\n    p_list = c(p_list, list(p))\n}\ncairo_pdf(filename = \".\/temp_plot.pdf\", onefile = TRUE)\nfor (p in p_list) {\n    print(p)\n}\ndev.off()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then the pages of PDF seem normal.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">References:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/github.com\/hms-dbmi\/UpSetR\/issues\/90\">https:\/\/github.com\/hms-dbmi\/UpSetR\/issues\/90<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/github.com\/wilkelab\/cowplot\/issues\/24\">https:\/\/github.com\/wilkelab\/cowplot\/issues\/24<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/blog.csdn.net\/weixin_34032827\/article\/details\/85900976\">https:\/\/blog.csdn.net\/weixin_34032827\/article\/details\/85900976<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>When I was generating a a series of plots using package &#8230; <a title=\"Prevent foremost blank page when plotting to PDF in R (with survminer)\" class=\"read-more\" href=\"https:\/\/ukyoi.info\/wordpress\/?p=10929\" aria-label=\"\u9605\u8bfb Prevent foremost blank page when plotting to PDF in R (with survminer)\">\u9605\u8bfb\u66f4\u591a<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-10929","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/ukyoi.info\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/10929","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ukyoi.info\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ukyoi.info\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ukyoi.info\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ukyoi.info\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10929"}],"version-history":[{"count":4,"href":"https:\/\/ukyoi.info\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/10929\/revisions"}],"predecessor-version":[{"id":10934,"href":"https:\/\/ukyoi.info\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/10929\/revisions\/10934"}],"wp:attachment":[{"href":"https:\/\/ukyoi.info\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10929"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ukyoi.info\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10929"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ukyoi.info\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10929"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}