5.1 Tagged Image File Format

In all of the following chapters, we will use very basic lattice and ggplot2 plot objects:

p_lattice <- xyplot(price ~ carat, data = diamonds)
p_ggplot <- ggplot(aes(x = carat, y = price), data = diamonds) +
  geom_point()

Okay, so we have our basic plot objects that we want to export as .tiff images. Note that graphics of points and lines are usually preferred as vector graphics (.eps or .pdf), but for the sake of demonstration, we will not care about this right now and export our scatter plot as .tiff anyway (.eps and .pdf examples follow). In all the examples that follow, we will produce figures that are of maximum width (17.35 cm) and height (23.35 cm) according to the PLOS ONE specifications. Furthermore, we will always first see how this is done with lattice, then with ggplot2.

For .tiff artworks, the default settings are as follows (PLOS ONE requirements in brackets):

  • width and heigth: 480 by 480 (max. 2049 by 2758)
  • units: pixels, or "px" (n.a.)
  • pointsize: 12 (6 - 12)
  • compression: "none" (LZW)
  • bg (background): "white" (white)
  • res (resolution): n.a., which basically means 72 ppi (300 - 600 ppi)
  • type: system dependent (check with getOption("bitmapType"))
  • family: system dependent, on Linux X11 it is "Helvetica" (Arial, Times, Symbol)

If we want to use units different from pixels for our width and height specifications, we need to supply a target resolution through res = ....

So, the first thing to do is open the tiff() device. In order to comply with PLOS ONE, we set

  • width to 17.35,
  • height to 23.35,
  • res to 300, and
  • compression to “lzw”.
tiff("test_la.tif", width = 17.35, height = 23.35, units = "cm", res = 300,
     compression = "lzw")

then, we render our plot object:

print(p_lattice)

and finally, we close our device:

dev.off()

This will create a .tiff image of our plot with a text point size of 12 for the axis labels, a point size of 10 for the axis tick labels and a point size of 14 for the plot title (if supplied). As we have seen, both lattice and ggplot2 ignore any parameter passed to the device via pointsize = .... Therefore, in case we want to change the point size of the text in our plot, we need to achieve this in another way.

In the following setup we will change the default font size to 20 pt and the axis tick labels to italic.

tiff_theme <- trellis.par.get()
tiff_theme$fontsize$text <- 20
tiff_theme$axis.text$font <- 3

print(update(p_lattice, par.settings = tiff_theme))

In order to export the graphic we simply wrap the above code between our tiff() and dev.off() calls (note here that we change the default font family to “Times” - check the exported image):

tiff("test_la.tif", width = 17.35, height = 23.35, units = "cm", res = 300,
     compression = "lzw")

tiff_theme <- trellis.par.get()
tiff_theme$fontsize$text <- 20
tiff_theme$axis.text$font <- 3

print(update(p_lattice, par.settings = tiff_theme))

invisible(dev.off())

This, however, does change the axis label text to a point size of 20, but the axis ticks are labelled with a point size of 16. This is because lattice uses so-called “character expansion” (short cex) factors for different regions of the plot. Axis tick labels have cex = 0.8 and the title has cex = 1.2. Therefore, the tick labels will be fontsize * cex (i.e. 20 * 0.8) in point size. We can, however, change this as well.

In the following we will change the axis font size to 10 and the axis tick label font size to 17.5:

tiff_theme <- trellis.par.get()

tiff_theme$fontsize$text <- 12 # set back to base fontsize 12

tiff_theme$par.xlab.text$cex <- 10/12
tiff_theme$par.ylab.text$cex <- 10/12

tiff_theme$axis.text$cex <- 17.5/12

print(update(p_lattice, par.settings = tiff_theme))

The same also applies if you use panel.text() or panel.key(). Use the cex parameter to adjust the font size you want your text to be.

Okay, so much for lattice. Let’s see how we can change things in ggplot2. The equivalent to lattice’s par.settings are the different themes in ggplot2. We have already seen this in the text size queries in Section 5. However, the way we set the font size is not at all equivalent as we are not allowed to assign a new value to a particular theme as theme_bw()$text$size <- 5. Instead, ggplot2 provides functionality to set the font size via theme_set() with the parameter base_size:

theme_set(theme_bw(base_size = 25))

print(p_ggplot +
        theme_bw())

Apart from theme_set() which changes the theme globally, there is also a function called theme_update() which changes parameters for the current theme in use. Once you change to a different theme, these settings will be neglected.

Note, we need to supply the absolute point sizes to these functions, not the relative expansion factors!

theme_set(theme_bw(base_size = 10))
theme_update(axis.text = element_text(size = 17.5, face = "italic"))

print(p_ggplot)

print(p_ggplot + 
        theme_grey())

The export procedure will then be equivalent to before:

tiff("test_gg.tif", width = 17.35, height = 23.35, units = "cm", res = 300,
     compression = "lzw")
theme_set(theme_bw(base_size = 10))
theme_update(axis.text = element_text(size = 17.5, face = "italic"))

print(p_ggplot)
invisible(dev.off())

Right, so now we know how to modify the standard settings shipped with both lattice and ggplot2. This will be pretty much the same for the other devices…