作者:Zuguang Gu
    翻译:Steven Shen
    原文:https://jokergoo.github.io/ComplexHeatmap-reference/book/a-single-heatmap.html#heatmap-as-raster-image

    2.8** Heatmap as raster image**

    Saving plots in PDF format is kind like best parctice to preserve the quality of the plots. However, when there are too many rows (say, > 10000), the output PDF file would be huge and it takes long time and memory to read the whole plot. On the other hand, details of the huge matrix will not be seen in limited size of PDF file. Rendering heatmaps (the heatmap body) as raster images will effectively reduce the file size while the plot looks exactly the same for your screen or if you print it out. In Heatmap() function, there are four options which control how to generate the raster image: use_raster, raster_device, raster_quality, raster_device_param.
    将绘图保存为 PDF 格式是保持图表质量的最佳选择。但是,当行数太多(例如 >10000)时,输出的 PDF 文件会很庞大,读取整个绘图需要很长时间和内存。另一方面,在有限大小的 PDF 文件中不会看到巨大矩阵的细节。将热图(热图主体)渲染为光栅图像将有效地减小文件大小,同时绘图看起来与您的屏幕完全相同,如果您把它打印出来的话。在 Heatmap() 函数中,有四个控制如何生成光栅图像的选项: use_rasterraster_deviceraster_qualityraster_device_param

    You can choose graphic device (png, jpeg and tiff) by raster_device, control the quality of the raster image by raster_quality, and pass further parameters for a specific device by raster_device_param.
    您可以通过 raster_device 选择图形设备( pngjpegtiff ),通过 raster_quality 控制光栅图像的质量,并通过 raster_device_param 向特定设备传递其他的参数。

    If raster_quality is set to 1, internally, a PNG (if raster_device is set to png) file is generated with the same physical size as the heatmap body and refit into the heatmap body as a raster image. The png file generated has the size of raster_quality*width and raster_quality*height. So a larger raster_quality value gives you a better reservation of the original resolution.
    如果 raster_quality 设置为 1,则在内部将生成一个 PNG(如果 raster_device 设置为 png )的文件,该文件与热图主体具有相同的物理大小,并作为光栅图像重新引入热图主体。 生成的 png 文件的大小为 raster_quality*widthraster_quality*height 。因此,更大的 raster_quality 值可以更好地保留原始分辨率。

    1. # code only for demonstration
    2. Heatmap(mat, use_raster = TRUE, raster_quality = 2)

    In Complexheatmap, use_raster is by default turned on if the number of rows or columns is more than 2000.
    Complexheatmap 中,如果行数或列数超过 2000,则默认情况下会打开 use_raster

    Following example compares the PDF file size with raster image by different devices.
    以下示例将 PDF 文件大小与不同设备的光栅图像进行比较。

    1. set.seed(123)
    2. mat2 = matrix(rnorm(10000*100), ncol = 100)
    3. pdf("heatmap.pdf", width = 8, height = 8)
    4. Heatmap(mat2, cluster_rows = FALSE, cluster_columns = FALSE, use_raster = FALSE)
    5. dev.off()
    6. pdf("heatmap_raster_by_png.pdf", width = 8, height = 8)
    7. Heatmap(mat2, cluster_rows = FALSE, cluster_columns = FALSE, use_raster = TRUE,
    8. raster_device = "png")
    9. dev.off()
    10. pdf("heatmap_raster_by_jpeg.pdf", width = 8, height = 8)
    11. Heatmap(mat2, cluster_rows = FALSE, cluster_columns = FALSE, use_raster = TRUE,
    12. raster_device = "jpeg")
    13. dev.off()
    14. pdf("heatmap_raster_by_tiff.pdf", width = 8, height = 8)
    15. Heatmap(mat2, cluster_rows = FALSE, cluster_columns = FALSE, use_raster = TRUE,
    16. raster_device = "tiff")
    17. dev.off()
    18. pdf("heatmap_raster_by_CairoPNG.pdf", width = 8, height = 8)
    19. Heatmap(mat2, cluster_rows = FALSE, cluster_columns = FALSE, use_raster = TRUE,
    20. raster_device = "CairoPNG")
    21. dev.off()
    22. pdf("heatmap_raster_by_CairoJPEG.pdf", width = 8, height = 8)
    23. Heatmap(mat2, cluster_rows = FALSE, cluster_columns = FALSE, use_raster = TRUE,
    24. raster_device = "CairoJPEG")
    25. dev.off()
    1. all_files = c("heatmap.pdf", "heatmap_raster_by_png.pdf",
    2. "heatmap_raster_by_jpeg.pdf", "heatmap_raster_by_tiff.pdf",
    3. "heatmap_raster_by_CairoPNG.pdf", "heatmap_raster_by_CairoJPEG.pdf")
    4. fs = file.size(all_files)
    5. names(fs) = all_files
    6. sapply(fs, function(x) paste(round(x/1024), "KB"))
    1. ## heatmap.pdf heatmap_raster_by_png.pdf
    2. ## "6583 KB" "374 KB"
    3. ## heatmap_raster_by_jpeg.pdf heatmap_raster_by_tiff.pdf
    4. ## "2845 KB" "374 KB"
    5. ## heatmap_raster_by_CairoPNG.pdf heatmap_raster_by_CairoJPEG.pdf
    6. ## "307 KB" "2975 KB"

    —— 本节完(创建:2019-06-14)——