Search icon CANCEL
Subscription
0
Cart icon
Cart
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Instant Heat Maps in R How-to

You're reading from  Instant Heat Maps in R How-to

Product type Book
Published in Jun 2013
Publisher Packt
ISBN-13 9781782165644
Pages 72 pages
Edition 1st Edition
Languages
Author (1):
Sebastian Raschka Sebastian Raschka
Profile icon Sebastian Raschka
Toc

Exporting for presentation (Simple)


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.

Getting ready

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.

How to do it...

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()

How it works...

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.

Note

If you are interested to see how the SVG and PostScript code looks like, I encourage you to open the SVG and PostScript files that were created by this script in your favorite plain text editor (for example, TextEdit on Mac OS X or Notepad on Windows).

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:

  1. Reading data: For this recipe, we will use the 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.

  2. Setting up our own heatmap.2() function: Like we did in the Customizing heat maps recipe, we create our own derivative of the 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:

    1. Opening a graphics device.

    2. Calling a plotting function.

    3. Closing the graphics device.

  3. Creating image files: First, let us create a PNG file using the 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.

    Note

    Very often, the main reason why we cannot open a PDF file that we created in R is that we forgot to close the PDF graphics device after we finished plotting!

    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.

    Tip

    You do not have to download special software to view the SVG file. Simply open it in your favorite web browser.

    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.

    Note

    The difference between JPEG, PNG, and BMP is that BMP stores the image file without compression, PNG with lossless compression, and JPEG with a lossy compression, respectively.

    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.

    Tip

    Of course, we can increase the ppi amount of the raster graphics even further, however it depends on the purpose of the image and where it will be presented.

  4. More options: So if we have the choice between the different raster graphics formats, I recommend using PNG, since the files are way smaller then BMP files due to the lossless compression. Using JPEG over PNG should only be considered if file size really matters to you.

    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.

    Tip

    Similarly, we could also use a color name instead of transparent to create a colored background.

    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()

    Note

    The background of the image files in PDF format is transparent by default.

    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

 
 

x11()

X Window System (X11), default in Unix/Linux

 

quartz()

Quartz, default on Mac OS X

 

windows()

Default in Microsoft Windows

Raster graphics devices

 
 

jpeg()

Joint Photographics Experts Group (JPEG) image file with lossy compression

 

png()

Portable Network Graphics (PNG) image file with lossless compression

 

bmp()

Bitmap (BMP) image file with no compression

 

tiff()

Tagged Image File Format (TIFF) with optional compression

Vector image devices

 
 

pdf()

Adobe's popular Portable Documents Format (PDF)

 

svg()

XML-based Scalable Vector Graphics (SVG) format

 

postscript()

PostScript (PS) format

Other

 
 

xfig()

File format for Xfig vector graphics editor

 

pictex()

Graphics format for LaTex import

You have been reading a chapter from
Instant Heat Maps in R How-to
Published in: Jun 2013 Publisher: Packt ISBN-13: 9781782165644
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime