LSMA

LONDON SCHOOL OF MULTIOMICS & AI

Free course

Free course

0 of 25 lessons

Lesson 8 of 9

Module 3 · Figures that tell the truth

Publication-ready export

Publication-ready export

Publication-ready export

The last step decides whether a good figure stays good in print: size, resolution, file format, fonts and a description for readers who cannot see it.

⏱ 10 min readModule 3 · Figures that tell the truthFree
Lesson at a glance3 steps · about 10 min
  1. Two ways to store a picture
  2. Saving both versions in R
  3. A final checklist

Two ways to store a picture

Every image file stores a picture in one of two ways. A vector file stores instructions: "a line from here to here, a circle of this size, the word 'dose' in this font". The viewer redraws those shapes at whatever size you look at them, so edges stay sharp. A raster file stores a fixed grid of coloured squares, called pixels. Enlarge it and each pixel gets bigger, so curves and letters turn into visible steps. Plots are made of lines, points and text, which is exactly what vector formats describe well.

In Figures with ggplot2 you saved a plot with ggsave(), giving its width and height in inches. This lesson looks at what that file contains, and how to choose its format, size and resolution so that the figure survives being printed, uploaded or projected. The difference between the two kinds of file shows as soon as you zoom in: a PDF of a plot viewed at 800% still has crisp letters, while a PNG at the same zoom shows each pixel as a small square.

It also helps to separate two ideas that are often mixed up. A raster image has a fixed number of pixels, for example 2,126 across. Its resolution in dpi only says how large those pixels will be when printed: the same 2,126 pixels make an image 180 mm wide at 300 dpi, or 90 mm wide at 600 dpi. So "a 300 dpi image" means little until you also know the printed size, and raising the dpi setting in an image editor without adding pixels adds no detail.

FormatTypeUse for
PDF, SVG, EPSVector: shapes, sharp at any sizePlots and diagrams; preferred by most journals
TIFF, PNGRaster: pixelsPhotographs and microscopy; plots when a journal asks for them, at 300–600 dpi
JPEGRaster, lossyNever for plots: compression blurs lines and text

How to read this table

Each row is a family of file formats. Pick the first row for anything drawn by code, the second when a journal insists on pixels or the image is a photograph, and avoid the third for figures.

  • dpi (dots per inch) is how many pixels a raster image packs into each inch of printed width. More dots per inch means finer detail and a larger file.
  • Lossy compression makes a file smaller by throwing away detail the eye is unlikely to miss in a photograph. Around the sharp edges of text and thin lines it leaves faint smudges and blocks, called compression artefacts. PNG and TIFF can be saved without this loss, which is why they are the raster formats to use for plots.
  • A photograph or micrograph is raster by nature: it was captured as pixels, so there are no shapes to store. Keep such images at their original resolution and never re-save them as JPEG repeatedly, since each save loses a little more.

Vector files have limits too. Each point in a scatter plot is stored as a separate shape, so a plot of tens of thousands of genes can produce a PDF of many megabytes that is slow to open. Saving such a panel as a high-resolution PNG, or rasterising only the point layer while keeping text and axes as vectors (the ggrastr package does this), keeps the file manageable. EPS, the oldest of the vector formats, cannot store semi-transparency, so points drawn with alpha below 1 are left out; R's PostScript device warns when this happens. Prefer PDF unless a journal asks for EPS.

Publication-ready export · LSMA free course Left: a diagonal line and a point stored as shapes stay smooth when enlarged. Right: the same line stored as a grid of pixels turns into a staircase of squares when enlarged. 1 · VECTOR: SHAPES 2 · RASTER: PIXELS Smooth at any zoom: PDF, SVG, EPS Steps when enlarged: PNG, TIFF, JPEG
Both panels show a diagonal line enlarged many times. The vector version is redrawn from its description and stays smooth; the raster version can only show the pixels it was saved with. The dashed line marks where the true edge should run. Schematic, not to scale.

Saving both versions in R

The figure object fig from Multi-panel figures can be written to any format; ggsave() chooses the format from the file name's ending.

R
ggsave("figure1.pdf", fig, width = 180, height = 85, units = "mm")               # vector
ggsave("figure1.png", fig, width = 180, height = 85, units = "mm", dpi = 600)    # raster

What this does

  • The first line saves fig as figure1.pdf, a vector file, 180 mm wide and 85 mm tall. No dpi is given, because a vector file has no pixels to count.
  • The second line saves the same figure as figure1.png, a raster file, at the same printed size and 600 dpi, which is the top of the range in the table.
  • The text after each # is a comment reminding you which kind of file each line makes.

Saving both is a good habit: the PDF is the one to submit when vector files are accepted, and the PNG is ready when a journal, a slide or a web page needs pixels.

Three mistakes are common at this step. First, if you leave out the plot argument, ggsave() saves the last plot displayed, which may be a single panel you were checking rather than the assembled figure, so always name the object. Second, width and height are in inches unless you give units, so width = 180 without units = "mm" asks for a figure 180 inches wide. ggsave() refuses sizes above 50 inches unless you set limitsize = FALSE, so this mistake usually stops with an error rather than a giant file. Third, the RStudio plot pane is not the saved figure. Its size depends on your window, so text that looks small on screen may be right at 180 mm, and the reverse. Open the saved file to judge it.

The text size in the file is the one set in the theme. theme_classic(base_size = 11), used in Multi-panel figures, gives 11-point axis titles at the saved size; tick labels and legend text are drawn at 80% of the base size, about 9 points. That is why a base size of 9 to 11 points suits most journal figures saved at their printed width, and stays above the 6–7 point minimum in the checklist below.

Worked example · pixels needed

180 mm wide = 7.09 inches × 600 dpi = 4,252 pixels across
at 300 dpi: 2,126 pixels · a screenshot (about 1,000 pixels) is far too small for print

Step by step. First convert millimetres to inches: 180 ÷ 25.4 = 7.087 inches. Then multiply by the dots per inch: 7.087 × 600 = 4,252 pixels, and 7.087 × 300 = 2,126 pixels. The same steps for the height give 85 ÷ 25.4 = 3.35 inches, so about 2,008 pixels tall at 600 dpi.

What the answer means. A screenshot about 1,000 pixels wide, printed 180 mm wide, has only about 1,000 ÷ 7.09 = 141 pixels per inch, well under half of the 300 dpi minimum. It would look soft and its text jagged. Asking for 600 dpi doubles the pixels in each direction, so the file holds four times as many pixels in total.

Go deeper: fonts inside a PDF, and smaller TIFFs

R's standard pdf() device, which ggsave() uses for .pdf files, does not embed its default fonts: it names Helvetica and trusts the reader's computer to supply it. Most viewers substitute a similar font, but some journal upload systems reject files without embedded fonts. Adding device = cairo_pdf to the ggsave() call embeds the fonts actually used. It relies on the Cairo graphics library: on Windows and Linux it usually works straight away, while on macOS it also needs XQuartz installed, so test it once before a deadline.

Journals that ask for TIFF often limit file size. An uncompressed colour TIFF of 4,252 × 2,008 pixels takes about 25 MB. ggsave("figure1.tiff", fig, width = 180, height = 85, units = "mm", dpi = 600, compression = "lzw") applies lossless LZW compression, which shrinks plots with large plain areas a great deal without changing a single pixel.

A final checklist

  • Saved at the final printed size; no text smaller than about 6–7 points.
  • Axis labels with units; panel letters; a legend explaining every symbol, error bar and colour.
  • Colour-blind-safe palette, and meaning not carried by colour alone.
  • Every bar chart starts at zero; scales match across compared panels.
  • The figure is made by a script, kept with the data, so it can be rebuilt.
  • Alt text for posters, web pages and slides: one or two sentences saying what the figure shows, not how it looks.

Why each item is there

Each line of the checklist sums up one earlier lesson. The size and text limit come from Multi-panel figures: a figure shrunk after saving shrinks its text too. Units and legends let a reader interpret the figure without the paper's main text. The palette rule is Colour that works for everyone, the zero baseline and matching scales Honest axes and scales. A script makes the figure reproducible, which matters when a reviewer asks for one more sample or a corrected value.

Alt text (alternative text) is a short written description attached to an image. Screen readers, the software used by people who are blind or have low vision, read it aloud in place of the picture, and it appears if the image fails to load. Good alt text gives the finding: "Tooth length rises with vitamin C dose; orange juice gives longer teeth than ascorbic acid at the two lower doses." Weak alt text describes appearance: "A scatter plot with blue and orange dots."

Fonts matter too. Journals usually ask for a common sans-serif font such as Arial or Helvetica, and they want text to stay editable or embedded in the file, not turned into a picture of text. Keeping the figure in a vector format from a script satisfies most of these rules automatically.

To set the font, give the theme a family, for example theme_classic(base_size = 11, base_family = "Helvetica"). The font must be one the graphics device can find. For PDF, the standard device knows Helvetica, Times and Courier by name; other fonts usually need device = cairo_pdf, described in the box above. For PNG and TIFF, the devices in the ragg package, used as ggsave("figure1.png", fig, device = ragg::agg_png, ...), can use any font installed on your computer and render text consistently on Windows, macOS and Linux.

Before you submit, check the files rather than the code. Open the PDF and zoom to 400%: text and lines should stay sharp, and most PDF viewers list the fonts in the document properties, marked as embedded or not. Check the PNG's pixel dimensions against the worked example, about 4,252 × 2,008 at 600 dpi. Then view the figure at its true size in greyscale, to confirm that nothing depends on colour alone. The project in Remake a figure asks you to run exactly these checks.

Rougier, Droettboom & Bourne 2014, "Ten simple rules for better figures" (PLOS Computational Biology), covers most items on this checklist in a few pages. The requirement for alt text comes from the Web Content Accessibility Guidelines (WCAG) of the World Wide Web Consortium, which ask for a text alternative for every non-text element; see w3.org/WAI. Treat the numbers in this lesson, such as 300 to 600 dpi and 6 to 7 point text, as typical rather than universal. Journal requirements differ and change, and the author guide of your target journal overrides any general rule.

Template

Figure 1. (A) Tooth length in guinea pigs by vitamin C dose and source (n = 10 per group); points are individual animals and lines show group means. (B) Birth weight against mother's weight for 115 non-smokers and 74 smokers; lines are linear fits with 95% confidence bands.

Notice what this legend does. It names what was measured and in whom, gives the sample sizes (10 per group; 115 + 74 = 189 births), and says exactly what each point, line and band represents. A reader who sees only the figure and this legend can tell what the error bands mean without guessing. Use the same pattern for your own figures: one sentence per panel, starting with the panel letter.

Further reading · Module 3Weissgerber et al. 2015: why bar charts hide the data, with examples. · All reading for this course

Check your understanding

1. Which format keeps a plot sharp at any size?

2. Why save at the final printed size?

3. What should alt text describe?

Up next

Project

Remake a figure

Ask BioTutor

Create a free website with Framer, the website builder loved by startups, designers and agencies.