After the exploratory stage of our data analyses, it is likely that we want to present our graphical heat map representations to an audience. This can be via presentation software over a projector, embedded in a website, a poster printout, or an image in a journal article. In this recipe, we will learn about the different file formats for image export so that we can choose whatever is most suited for the purpose.
Download the 5644OS_05_01.r
script from your account at http://www.packtpub.com and save it to your hard drive.
For an explanation of how to run scripts in R, please read the Getting ready section of the Creating your first heat map in R recipe.
The script will check automatically if any additional packages need to be installed in R. You can find more information about the installation of packages in the Getting ready section of the Creating your first heat map in R recipe.
Execute the 5644OS_05_01.r
script in R and compare the different image files to other that which were written to the current working directory:
if (!require("gplots")) { install.packages("gplots", dependencies = TRUE) library(gplots) } if (!require("RColorBrewer")) { install.packages("RColorBrewer", dependencies = TRUE) library(gplots) } ### converting data data(co2) rowcolNames <- list(as.character(1959:1997), month.abb) co2_data <- matrix(co2, ncol = 12, byrow = TRUE, dimnames = rowcolNames) heat2 <- function(...) heatmap.2(co2_data, trace = "none", density.info = "none", dendrogram = "none", Colv = FALSE, Rowv = FALSE, col = colorRampPalette(c("blue", "yellow", "red")), (n = 100), margin = c(5,8), lhei = c(0.25,1.25), ...) png("1_PNG_default.png") heat2(main = "PNG default") dev.off() png("2_PNG_highres.png", width = 5*300, height = 5*300, res = 300, pointsize = 8) heat2(main = "PNG High Resolution") dev.off() jpeg("3_JPEG_highres.png", width = 5*300, height = 5*300, res = 300, pointsize = 8) heat2(main = "JPEG default") dev.off() bmp("4_BMP_default.bmp", width = 5*300, height = 5*300, res = 300, pointsize = 8) heat2(main = "BMP default") dev.off() pdf("5_PDF_default.pdf", width = 5, height = 5, pointsize = 8) heat2(main = "PDF default") dev.off() svg("6_SVG_default.svg", width = 5, height = 5, pointsize = 8) heat2(main = "SVG default") dev.off() svg("7_PostScript_default.ps", width = 5, height = 5, pointsize = 8) heat2(main = "PostScript default") dev.off() png("8_PNG_transp.png", width = 5*300, height = 5*300, res = 300, pointsize = 8, bg = "transparent") heat2(main = "PNG Transparent Background") dev.off() pdf("9_PDF_mono.pdf", family = "Courier", paper = "USr") heat2(main = "PDF Monospace Font") dev.off()
There are two major classes of image formats that we can choose from when we want to save our plots as a file: vector graphics and raster graphics. Raster graphics, also known as bitmaps, comprise popular image formats such as PNG, BMP, and JPEG. These file formats store information of each individual pixel, thus the quality of the image heavily depends on the resolution, that is pixel per inch (ppi). However, high-resolution images come with the additional cost of a large file size. Nowadays, we usually do not have to worry about limited storage space of a hard drive anymore, but large images are not suitable for the Web, and in the worst case, they can also have a negative impact on the responsiveness of your presentation software.
One of the nice features of R is to save plots as vector graphics, such as SVG, PDF, and PostScript. While all three vector graphic formats offer high quality graphics, each has its own area of application. SVG can be easily embedded into HTML code and is the format of choice for displaying your plots on the Web. PostScript is the desired format when you want to send articles to a journal, where as PDF is well known for its great compatibility with a wide range of software including PDF readers.
Rather than storing information of each pixel in a grid, vector graphic files contain instructions for geometrical shapes that are used by the visualization software to render the image. This allows us to zoom in to the image without any loss of quality. Another advantage of vector images is that they generally have a much smaller file size than raster graphics. The only exception is when your plot is heavily over-plotted, so that a lot of instructions have to be saved in the vector graphic file. Generally, vector graphics are great to store image files on your computer, however images can only be rendered by certain software and are converted to raster graphics for printing.
Images from SVG files, for example, can be rendered in every modern web browser, and since they are saved as Extensible Markup Language (XML) code, they can be easily embedded into HTML, which makes SVG the format of choice if you want to display your graphics on a website.
If you do not consider to embed your graphic in a website or send your graphics off to a journal for professional publication, I recommend using the PDF format, since it has great compatibility with other software and offers best quality output in reasonably small file sizes.
You will find an overview table of the different graphic devices that are available in R at the end of this section.
Now let us dive in and create different image files from our heat maps:
co2
data set from the data
package in R. This co2
data set is a time-series that consists of 468 monthly measurements of carbon dioxide (CO2) concentrations in parts per million (ppm) from 1959 to 1997.We know from the Creating your first heat map in R recipe how to convert data from a time-series format into a numeric matrix, which is the only format that is compatible with the heatmap2.()
function.
heatmap.2()
function with some default arguments that we want to use for all our heat maps in this recipe to avoid repetitive typing efforts:heat2 <- function(...) heatmap.2(co2_data, trace = "none", density.info = "none", dendrogram = "none", Colv = FALSE, Rowv = FALSE, col = colorRampPalette(c("blue", "yellow", "red")), (n = 100), margin = c(5,8), lhei = c(0.25,1.25), ...)
We use a new parameter lhei
that we have not encountered so far. With this parameter, we can control the height of the different plot elements. The arguments we provide here will make our legend thinner.
Before we proceed with the graphic devices, let us briefly take a look at the general paradigm of creating image files in R, which consists of three basic steps as follows:
png()
function with its default parameters:png("1_PNG_default.png") heat2(main = "PNG default") dev.off()
The first argument that the graphics device takes is the name of the output file. After we open the graphics device, we create the heat map using our heat2()
function, and lastly, we close the graphic device with the dev.off()
function.
Because the resolution of the PNG image is very low (75 ppi) when we use png()
with its default parameters, we create another PNG file with a resolution of 300 ppi, which should provide reasonable quality for a print out on a letter-sized piece of paper. The default size of png()
is measured in pixels, and here we are creating a 5 x 5 inches output by taking the number of pixels per inch and multiplying it by 5 inches. Further, we decrease the text size slightly from 12 to 8 bp (1 bp equals 1/72 inches) in the pointsize
parameter:
png("2_PNG_highres.png", width = 5*300, height = 5*300, res = 300, pointsize = 8) heat2(main = "PNG High Resolution") dev.off()
Next, we create a JPEG, BMB, SVG, and PDF file with the same parameters so we can compare them to each other. Note that the height and width of the SVG and PDF files are measured in inches.
When we compare those different image files to each other, we see that they all show the same heat map, but if we zoom in, we notice that the quality differs tremendously.
In the following image, you can see the tremendous difference in the image quality between the different file formats. This is something you should consider, especially if you are preparing your graphics for an on-screen presentation.
When we view the BMP, PNG, and JPEG images, the quality seems to be quite reasonable. But as soon as we zoom in on the image, we can barely read the text of the PNG file with the 75 ppi default. In contrast to the 300 ppi BMP, PNG and JPEG files do not have any jagged edges in the PDF and SVG files, no matter how far we zoom in.
In contrast to JPEG files, BMP and PNG files support transparent backgrounds. This is particularly useful if we want to place the heat map on a patterned background, on a poster, or a presentation slide for example. This can be specified by providing the argument transparent
for the bg
(background) parameter.
png("test/8_PNG_transp.png", width = 5*300, height = 5*300, res = 300, pointsize = 8, bg = "transparent") heat2(main = "PNG Transparent Background") dev.off()
The PDF format has the further advantage wherein we can choose a different font family if we like. We can choose from AvantGarde
, Bookman
, Courier
, Helvetica
, Helvetica-Narrow
, NewCenturySchoolbook
, Palatino
, and Times
. Also, we can specify the paper format, such as a4
for DIN A4, letter
for the American Standard Letter format, or a4r
and USr
for the respective rotated landscape formats:
pdf("9_PDF_mono.pdf", family = "Courier", paper = "USr") heat2(main = "PDF Monospace Font") dev.off()
The following table shows the different graphic devices that are available in R:
On-screen devices | ||
---|---|---|
|
X Window System (X11), default in Unix/Linux | |
|
Quartz, default on Mac OS X | |
|
Default in Microsoft Windows | |
Raster graphics devices | ||
|
Joint Photographics Experts Group (JPEG) image file with lossy compression | |
|
Portable Network Graphics (PNG) image file with lossless compression | |
|
Bitmap (BMP) image file with no compression | |
|
Tagged Image File Format (TIFF) with optional compression | |
Vector image devices | ||
|
Adobe's popular Portable Documents Format (PDF) | |
|
XML-based Scalable Vector Graphics (SVG) format | |
|
PostScript (PS) format | |
Other | ||
|
File format for Xfig vector graphics editor | |
|
Graphics format for LaTex import |