Saving graphs

Suggestions for saving graphs for making figures.


ggsave for saving plots

Saving graphs requires a lot of trial and error! These are just the options that work for me. You’ll have to try a few and decide.

Generally, final figures have panels of around 4-5 cm height and varying width depending on the number of groups along the X-axis. The final font size should be around 7-8 pt, line sizes should be around 1 pt and symbols 3-4 pt. Font typeface should be sans serif (Arial is perfect) without bold or italics (as these are difficult to read on graphs).

The other important thing to consider is the resolution. Graphs saved as “.svg” or “.pdf” are vectorised and essentially do not lose resolution when zoomed in. For “.png” or “.tiff”, you will need to set the “dpi” at 1200 as required by most journals. I usually save graphs as .pdf or .svg files and then import them into other software for assembling all figure panels (e.g., Inskscape or Powerpoint or others).

ggsave from ggplot2 is great for saving graphs. This is what I use. It can save the last plot into a new file. If you saved your plot as an object, you can ask the object to be saved into a file.

#plot a graph
p <- plot_scatterbox(data_1w_death, 
                     Genotype,
                     Death)

Save the graph with following starting options.

ggsave(filename = "plot1.svg",    #file name
       plot = p,                  #plot created above
       height = 8,    #height when no title on graph
       width = 13,    #width for 3 groups + legend
       units = "cm")  #units in cm

This is how the above graph will look at ~4 cm height, which is often the height in a printed journal.

When you add a title to the graph, consider increasing the height by 2 cm to 10 cm to accommodate it.

p <- plot_scatterbox(data_1w_death, 
                     Genotype,
                     Death)+
  labs(title = "Genotype vs Death")

ggsave(filename = "plot2.svg",    #file name
       plot = p,                  #plot created above
       height = 10,   #height when no title on graph
       width = 13,    #width for 3 groups + legend
       units = "cm")  #units in cm

This is how the graph will look at ~5cm height. The rest of the graph scales roughly similarly to the one above.

Note that saving the same graph as above at twice the size does not scale the font or symbol sizes.

This is how the above graph saved at height 20 cm x width 26 cm would look.

As you see, the box and whiskers have taken up the plot space, but font and symbol sizes and line widths have not increased! The “effective” font size is much smaller. This is why “scaling” the relative sizes of symbols, fonts and line widths in ggplot2 needs several tries for new graphs! To get the best relative sizes for elements inside the graph, try changing font size, line widths, symbol sizes etc. to make it look good in the final dimensions you need for it within the Figure panel.

The default font size in ggplot2 is 12, which in my experience is too small for graphs of average sizes even in outputs from Rmarkdown/knitr or in the Plots pane in RStudio. This is why the default font size in grafify is 20. The line widths scale at 20/22 (which is the same ratio as ggplot2 default i.e., base font size/22). The default width of boxes and bars and other options are set in grafify such that graphs saved at above dimensions turn out well for making figure panels.

Nonetheless, I still have to try several sizes to get the best scaling of items within the plot. For instance, when some graphs have just two groups along the X-axis and others have 3 or more, the width of the saved file will have to proportionately increase. This can take some tries depending on how wide the legend and the text along the Y-axis are.

In the end, the problem is rarely about getting a single graph to look nice – this is relatively simple. What can take time and effort is to make all graphs within a single Figure look consistent (i.e., with similar sizes of symbols, fonts, line widths etc.).