如何在R中保存使用forestplot包创建的图?

时间:2018-12-04 16:40:43

标签: r forestplot

我已经使用R中的forestplot包创建了一个森林图,如下所示:

own <- fpTxtGp(ticks = gpar(cex = 0.65), xlab = gpar(fontsize = 16))

xticks <- seq(from = -0.25, to = 1, by = 0.25)

fn <- local({
  i = 0
  no_lines <- sum(!is.na(forest$mean))
  b_clrs = colorRampPalette(colors=c("#21538A", "#21538A", "#A2B6D3", "#A2B6D3", "#DAE0EC"))(no_lines)

  function(..., clr.marker){
    i <<- i + 1
    fpDrawCircleCI(..., clr.marker = b_clrs[i])
  }
})

fplot <- forestplot::forestplot(text, 
                                fn.ci_norm = fn,
                                lower = forest$low, 
                                upper = forest$high, 
                                mean = forest$mean, 
                                is.summary = c(TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, TRUE),
                                xlab = "SPI-CY risk score difference (SD)",
                                col = fpColors(line = "darkgrey", summary = "#DAE0EC", zero = "black"), 
                                vertices = TRUE, 
                                grid = TRUE,
                                boxsize = .35,
                                txt_gp = own,
                                xticks = xticks,
                                lwd.zero = 1.25
                                ) 

我想直接从我的R笔记本中保存情节。我通常使用ggsave保存地块,但是森林地块是网格对象,因此无法正常工作:

ggsave("forestplot.png", height = 5, width = 7, dpi = 600)

我试图用pdf()保存它,但这也不能直接在我的笔记本上工作。欢迎任何建议。

1 个答案:

答案 0 :(得分:0)

要直接从R笔记本中保存绘图(由于user2554330的注释,我添加了绘图初始化):

# initialize plot
png("forestplot.png", width=480, height=480)

# make plot
fplot

# save plot
dev.copy(png, "forestplot.png")
dev.off()