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

How to save a plot created with forestplot package in R?

我使用 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() 保存它,但这也不能直接从我的笔记本上运行。欢迎提出任何建议。

直接从你的 R notebook 保存绘图(由于 user2554330 的评论,我添加了绘图初始化):

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

# make plot
fplot

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