7  Themes, fonts, sizes and saving

This chapter covers tidying up the appearance unrelated to the data (background, lines, text) and saving at the size required by a journal’s guidelines.

7.1 Choosing a theme and changing its elements

In ggplot2, you choose the overall theme with theme_xxx() and change individual elements with theme().

Element Function to change it Example
Text element_text() axis.title = element_text(size = 11, face = "bold")
Lines element_line() panel.grid.major = element_line(colour = "grey90")
Rectangles element_rect() panel.background = element_rect(fill = "white")
Remove element_blank() panel.grid.minor = element_blank()

In matplotlib, you change the overall look with plt.rcParams (a dictionary of defaults) and individual parts with ax. functions. seaborn’s set_theme() / set_style() are functions that change rcParams in bulk.

7.2 Displaying Japanese text on your own PC

Japanese text in ggplot2 depends on the graphics device and the font name.

# macOS
theme_set(theme_minimal(base_family = "Hiragino Sans"))
# Windows
theme_set(theme_minimal(base_family = "Yu Gothic"))
# Independent of the environment: ragg (RStudio's default device) or showtext
library(ragg)
ggsave("fig.png", p, device = ragg::agg_png, width = 85, height = 65, units = "mm", dpi = 300)

When saving as PDF, use cairo_pdf, which can embed Japanese (with the default pdf(), Japanese text may not be displayed).

ggsave("fig.pdf", p, device = cairo_pdf, width = 85, height = 65, units = "mm")

In matplotlib, you register a font file and specify it by name. This is the same method as this hands-on’s py/setup.py.

from matplotlib import font_manager
font_manager.fontManager.addfont("NotoSansJP-Regular.ttf")   # font file
plt.rcParams["font.family"] = "Noto Sans JP"
plt.rcParams["axes.unicode_minus"] = False                   # prevent the minus sign from rendering as a missing glyph

To use fonts installed in the OS, set font.family to "Hiragino Sans" (macOS), "Yu Gothic" (Windows), "Noto Sans CJK JP" (Linux), etc. When characters turn into □ (“tofu”), check whether a Glyph ... missing from font warning was shown.

To embed Japanese in a PDF so that the text can be edited in Illustrator and the like, set it to embed as TrueType.

plt.rcParams["pdf.fonttype"] = 42   # with the default 3, text is embedded as shapes and is hard to edit
plt.rcParams["ps.fonttype"] = 42

7.3 Making figures at the size required by journal guidelines

Journal figure widths are roughly 85–90 mm for a single column and 170–180 mm for two columns. Text is typically required to be about 6–9 pt when printed. What matters is to make the figure at its actual size from the start. If you make it large and shrink it, text and lines become too small to read.

The 8 in theme_paper(base_size = 8) is in points (pt). If you fix the actual size of the figure when saving, the text will be 8 pt when printed.

ggsave("fig1.pdf", width = 85, height = 65, units = "mm", device = cairo_pdf)   # vector
ggsave("fig1.tiff", width = 85, height = 65, units = "mm", dpi = 600, compression = "lzw")   # raster
fig.savefig("fig1.pdf", bbox_inches="tight")                      # vector
fig.savefig("fig1.tiff", dpi=600, pil_kwargs={"compression": "tiff_lzw"})  # raster

bbox_inches="tight" trims the margins, but note that the figure becomes smaller than the specified size. To keep the size exactly, adjust the margins with fig.tight_layout() or layout="constrained" and then save without specifying bbox_inches.

7.4 File formats

Format Type When to use
PDF / SVG / EPS Vector (no degradation when enlarged) Figures for papers (lines, text, points). Can be edited later in Illustrator, etc.
PNG Raster (lossless compression) Slides and the web. Specify the resolution (dpi)
TIFF Raster Sometimes required by journals (300–600 dpi; e.g. 300 for photos, 600–1200 for line art)
JPEG Raster (lossy compression) Don’t use for figures (lines and text get blurry edges)
TipFigures with tens of thousands of points

Saving a scatter plot with tens of thousands of points as PDF makes the file heavy and slow to display. With ggrastr in ggplot2, or ax.scatter(..., rasterized=True) in matplotlib, you can rasterize only the points while keeping text and axes as vectors.

7.5 About saving inside the browser

R and Python on this page run inside the browser, so files saved with ggsave() or savefig() are created in a virtual folder inside the browser and are not saved to your PC. You can still check the size of the saved files.

ImportantChapter summary
  • In ggplot2, change the whole with theme_xxx() and elements with theme() and element_xxx(). Make your own theme a function
  • In matplotlib, change things with rcParams (temporarily with rc_context) and ax.. seaborn’s set_theme() changes rcParams in bulk
  • For Japanese text: in R, the font name and graphics device (ragg, cairo_pdf); in matplotlib, registering the font and pdf.fonttype = 42
  • Make figures at their actual size (mm) and text size (pt) from the start. Vector (PDF/SVG) for papers; specify dpi for raster