曼哈顿情节以透明背景保存

时间:2018-12-18 12:47:59

标签: r ggplot2 themes bioinformatics genetics

拥有曼哈顿情节,尝试使用ggsave保存它:

library(qqman)
MH <- manhattan(gwasResults2, chr="CHR", bp="BP", snp="SNP", p="P", 
                     col = c("chartreuse2", "darkorange1", "gold1"),ylim=c(0,-log10(1e-06)), chrlabs = NULL,
                     suggestiveline = -log10(1e-03), genomewideline = -log10(1e-05),
                     highlight = NULL, logp = TRUE, annotatePval = NULL,
                     annotateTop = TRUE, main='DWStem') + theme_bw() + theme(panel.background = element_rect(fill = "transparent",colour = NA),
                                                                             plot.background = element_rect(fill = "transparent",colour = NA))
ggsave("DWSte.png",MH, bg = "transparent")

该文件已生成,但完全空白,我的意思是情节没有来临。

有什么主意!?

谢谢:)

1 个答案:

答案 0 :(得分:1)

似乎manhattan包中的qqman功能与ggplot2:ggsave不兼容,因为它使用的是基本图形而不是grid图形。如果需要,请使用manhattan检查getAnywhere(manhattan)函数后面的代码。

尝试以下方法:

library(qqman)
library(grid)
library(gridGraphics)
library(ggplot2)

# your plot, but without the ggplot2 theme lines (they do nothing anyways)
manhattan(gwasResults, chr="CHR", bp="BP", snp="SNP", p="P", 
          col = c("chartreuse2", "darkorange1", "gold1"),
          ylim = c(0, -log10(1e-06)), chrlabs = NULL,
          suggestiveline = -log10(1e-03), genomewideline = -log10(1e-05),
          highlight = NULL, logp = TRUE, annotatePval = NULL,
          annotateTop = TRUE, main='DWStem')
# Transform the base plot to grobs, so that is compatible with grid and, therefore ggsave
p <- recordPlot()
g <- grid.grabExpr(grid.echo(p))
ggsave("DWSte.png", g, bg = "transparent")

或者,this tutorial似乎提供了ggplot2解决方案。