Search code examples
rtiffr-forestplot

Not able to print forestplot in high resolution format in R


I need to create a forestplot of high resolution. I used the forestplot() function from library(forestplot) to create my plot, and then attempted to use the tiff() function to create a high resolution image for publication. However, my image turned blank.

It works if I export directly from R but not as high resolution as it was supposed to.

library(forestplot)

df <- structure(list(
  mean = c(NA, 0.22, 0.20, 0.27),
  lower = c(NA, 0.05, 0.04, 0.01),
  upper = c(NA, 0.95, 1.08, 9.12)),
  .Names = c("mean", "lower", "upper"),
  row.names = c(NA, -4L),
  class = "data.frame")

tabletext <- cbind(
  c("", "Pooled", "Group 1", "Group 2"),
  c("N", "4334", "3354", "980"),
  c("HR (95% CI)", "0.22 (0.05, 0.95)", "0.20 (0.04, 1.08)", "0.27 (0.01, 9.12)"),
  c("p-value", "0.042", "0.061", "0.467")
)

ggfp <- forestplot(tabletext,
                   df,
                   new_page = TRUE,
                   is.summary = c(TRUE, rep(FALSE, 3)),
                   clip = c(0, 2),
                   colgap = unit(5, "mm"),
                   line.margin = unit(2, "mm"),
                   lineheight = unit(1, "in"),
                   txt_gp = fpTxtGp(label = gpar(cex = 1),
                                    ticks = gpar(cex = 1)),
                   align = c("l", "c", "c", "c"),
                   boxsize = 0.2,
                   xticks = seq(0, 2.0, 0.5), 
                   zero = 1,
                   col = fpColors(box = "royalblue",
                                  line = "darkblue"),
                   mar = unit(c(-1, 0.5, -2, 0.5), "in"))

tiff("forestplot.tiff", units = "in", width = 9, height = 7, res = 300)
ggfp
dev.off()

The file was created but it was a blank page


Solution

  • This works for me (output file is 17MB):

    library(forestplot)
    
    setwd("/path/to/directory/for/plot")
    
    df <- structure(list(
      mean = c(NA, 0.22, 0.20, 0.27),
      lower = c(NA, 0.05, 0.04, 0.01),
      upper = c(NA, 0.95, 1.08, 9.12)),
      .Names = c("mean", "lower", "upper"),
      row.names = c(NA, -4L),
      class = "data.frame")
    
    tabletext <- cbind(
      c("", "Pooled", "Group 1", "Group 2"),
      c("N", "4334", "3354", "980"),
      c("HR (95% CI)", "0.22 (0.05, 0.95)", "0.20 (0.04, 1.08)", "0.27 (0.01, 9.12)"),
      c("p-value", "0.042", "0.061", "0.467")
    )
    
    tiff("forestplot.tiff", units = "in", width = 9, height = 7, res = 300)
    
    forestplot(tabletext,
                       df,
                       new_page = TRUE,
                       is.summary = c(TRUE, rep(FALSE, 3)),
                       clip = c(0, 2),
                       colgap = unit(5, "mm"),
                       line.margin = unit(2, "mm"),
                       lineheight = unit(1, "in"),
                       txt_gp = fpTxtGp(label = gpar(cex = 1),
                                        ticks = gpar(cex = 1)),
                       align = c("l", "c", "c", "c"),
                       boxsize = 0.2,
                       xticks = seq(0, 2.0, 0.5), 
                       zero = 1,
                       col = fpColors(box = "royalblue",
                                      line = "darkblue"),
                       mar = unit(c(-1, 0.5, -2, 0.5), "in"))
    dev.off()