A vector graphic stores the chart
as shapes and text instead of pixels, so it stays sharp however far it
is zoomed in and however large it is printed. plot_normal()
returns a ggplot object, so ggplot2::ggsave() saves it in
any of the vector formats below: the file extension names the format,
and device chooses how the file is written.
The chart below is itself an SVG: zoom into the page and it stays sharp.
| Where the chart goes | Format | device |
|---|---|---|
| LaTeX, journals, printing | cairo_pdf |
|
| Web pages, Markdown, Word and PowerPoint | SVG | svg |
| Inkscape or Illustrator, to edit the text | SVG | svglite::svglite |
| Older versions of Word and PowerPoint | EMF | devEMF::emf |
| Journals that ask for EPS | EPS | cairo_ps |
width and height set the size of the
figure, in inches unless units says otherwise. The chart
keeps its proportions, about 2.2 times as wide as it is tall with the
standard scales, so a figure with those proportions has no empty
margins:
ggsave("normal.pdf", plot_normal(), width = 7, height = 3.2, device = cairo_pdf)
ggsave("normal.pdf", plot_normal(), width = 17.8, height = 8.2, units = "cm", device = cairo_pdf)A vector graphic scales as a whole: shrink it in a document and its
text shrinks with it. So save it at the size it will be shown, and set
size, the text size in millimetres, for that width. The
default of 3, about 8.5 points, suits a width of about 7 inches (17.8
cm); for other widths, scale it in proportion,
size = 3 * width / 7 with the width in inches. A poster or
a wide slide:
Below about 6 inches (15 cm) the text becomes small for print, so the chart reads best across the full width of a page.
Every scale adds a row, and long scale names take width from the
chart, so the height that fits depends on both.
plot_normal_height() works it out for any chart at the
width you give, in inches, centimetres or millimetres:
For LaTeX, journals and printing. Use
device = cairo_pdf: it embeds the font in the file, so the
chart looks the same on every computer and printer, and journals that
require embedded fonts accept it. Without device,
ggsave() uses R’s pdf() device, which does not
embed the font: it names Helvetica and leaves the program that opens the
file to supply it.
In LaTeX, include it at the size it was saved, so that its text keeps its size:
Several charts go into one PDF, one per page, by opening the device yourself and printing each chart:
For web pages, Markdown files (GitHub shows an SVG in a README like any image) and Word and PowerPoint in Microsoft 365, which insert SVG files as pictures. There are two ways to write one.
device = svg, R’s own SVG device, draws every letter as
an outline. The chart looks exactly the same wherever it is opened and
no other package is needed, but its text cannot be selected or
edited:
The svglite package keeps the text as text, so it can be selected,
searched and edited in Inkscape or Illustrator. It is what
ggsave() uses for a .svg file when
device is left out, and it has to be installed first:
The program that opens the file draws the text in Arial, so on a computer without Arial another font takes its place and the labels may shift slightly.
For older versions of Word and PowerPoint, which take Windows metafiles but not SVG. The devEMF package writes them on any system:
Some journals still ask for EPS. Use device = cairo_ps,
which embeds the font:
EPS has no transparency. The chart itself has none, but a layer added
with alpha, such as the shaded tails in the Examples article, is turned into an image
inside the file by cairo_ps and left out altogether by
ggsave()’s default EPS device. Save such charts as PDF or
SVG instead.