High-resolution plots in GGPlot and RMarkdown: How to adjust font and element sizing

GGplot2
RMarkdown
Data visualization
Author

Catherine Moez

Published

June 5, 2023

When saving GGPlot2 plots in high resolution, you may run into problems with font and plot element sizing.

Here is a small sample dataset and an example of the text sizing and legend sizing changes needed for printing in 2000 DPI resolution.

First, the dataset and libraries:

# create example dataframe
p2 = data.frame(speakerparty = c(rep("Bloc Quebecois", 6),
                                 rep("Liberal", 6), 
                                 rep("New Democratic Party", 6),
                                 rep("Progressive Conservative", 6),
                                 rep("Reform", 6)),
                word = c("students", "merit", "scholarships", 
                         "sovereignty", "succeed", "need", # BQ
                         "canadians", "employees", "industry",
                         "senior", "board", "hockey", # Liberal
                         "corporate", "tax", "conservatives", 
                         "liberals", "canadians", "interests", # NDP
                         "gala", "corporate", "montreal",
                         "labour", "ontario", "companies", # PC
                         "grassroots", "band", "canadians",
                         "native", "liberals", "white"),
                n = c(8, 3, 3, 2, 2, 5,
                      19, 5, 7, 7, 4, 4, 
                      56, 14, 6, 9, 19, 8,
                      3, 6, 4, 4, 4, 3,
                      18, 13, 29, 7, 10, 10),
                tf = c(0.01238, 0.00464, 0.00464, 0.00309, 0.00309, 0.00773,
                       0.00930, 0.00244, 0.00342, 0.00342, 0.00195, 0.00195,
                       0.02480, 0.0062, 0.00265, 0.00398, 0.00841, 0.00354,
                       0.00433, 0.00867, 0.00578, 0.00578, 0.00578, 0.00433,
                       0.00529, 0.00382, 0.00853, 0.00205, 0.00294, 0.00294),
                tf_idf = c(0.01049, 0.00903, 0.00903, 0.00602, 0.00602, 0.00433,
                           0.005207, 0.00476, 0.00429, 0.00429, 0.0038, 0.0038,
                           0.02101, 0.00525, 0.00517, 0.0049, 0.0047, 0.0044,
                           0.00843, 0.00734, 0.00724, 0.00489, 0.00489, 0.00367,
                           0.01030, 0.0074, 0.00477, 0.0040, 0.00368, 0.00368))
# open plotting packages
library(ggplot2)
library(ggrepel)
# additional plotting packages, to change fonts (Optional):
library(showtext)
font_add_google("EB Garamond") # not in pkg as ttf
showtext_auto() # font changes (run once per session)

A simple GGplot in RMarkdown:

ggplot(data = p2,
       aes(x = tf, y = tf_idf, colour = speakerparty, label = word)) +
  geom_point(size = 0.8, alpha = 0.2) + 
  geom_text_repel() +
  ggtitle("TF-IDF Scores and Frequency of Use for Words in 'Elite*' Window Text") +
  scale_color_manual(values = c("#00B0F6", "#F8766D", "#00BF7D", 
                                "#C77CFF", "#E76BF3", "#A3A500")) +
  theme_bw()

Or, with the title fonts adjusted to Garamond:

ggplot(data = p2,
       aes(x = tf, y = tf_idf, colour = speakerparty, label = word)) +
  geom_point(size = 1, alpha = 0.2) + 
  geom_text_repel(size = 36) +
  ggtitle("TF-IDF Scores and Frequency of Use for Words in 'Elite*' Window Text") +
  scale_color_manual(values = c("#00B0F6", "#F8766D", "#00BF7D", 
                                "#C77CFF", "#E76BF3", "#A3A500")) +
  theme_bw() + # erases font change if 2nd...
  theme(plot.title = element_text(family = "EB Garamond", size = 360/.pt),
        plot.subtitle = element_text(family = "EB Garamond")) +
  guides(shape = guide_legend(override.aes = list(size = 600)),
         color = guide_legend(override.aes = list(size = 1))) # legend smaller

But when we save the plot to file, in high resolution (2000 DPI), the fonts will be much smaller than we want:

ggsave(filename = "tfidf_plot_test1.png",
       units = "in",
       width = 7,
       height = 4.375,
       dpi = 2000)

Problem High DPI image, RMD

Or, in Quarto, the default font sizes may end up too large:

Problem High DPI image, Quarto


To solve this, adjust the text elements’ size.

You may encounter the “Viewport has zero dimension(s)” error – if so, in R Markdown, change the chunk’s heading from {r} to {r, fig.width = 20} or a similarly high number. If the plot is too large to preview/generate in R Markdown, then it will not save to file.

The corrected plot’s code:

# change legend and text sizes
ggplot(data = p2,
       aes(x = tf, y = tf_idf, colour = speakerparty, label = word)) +
  geom_point(size = 1, alpha = 0.2) + 
  geom_text_repel(size = 36) + # Main text in the image -- can also use geom_text(size = 36)
  ggtitle("TF-IDF Scores and Frequency of Use for Words in 'Elite*' Window Text") +
  scale_color_manual(values = c("#00B0F6", "#F8766D", "#00BF7D", 
                                "#C77CFF", "#E76BF3", "#A3A500")) +
  theme_bw() + # erases font change if 2nd...
  theme(plot.title = element_text(family = "EB Garamond", size = 360/.pt), # Title text
        plot.subtitle = element_text(family = "EB Garamond"),
        axis.text=element_text(size=72), # Axis labels' text
        axis.title=element_text(size=90,face="bold"),
        legend.text=element_text(size=90), # Legend text
        legend.title=element_text(size=90,face="bold"),
        legend.spacing.x = unit(0.1, "in"), # Legend spacing
        legend.spacing.y = unit(0.1, "in")) +
  guides(color = guide_legend(override.aes = list(size = 1))) # Legend symbols' size
# to save
ggsave(filename = "tfidf_plot_test3.png",
       units = "in",
       width = 7,
       height = 4.375,
       dpi = 2000)

Returning an image like:

Successful High DPI image

That’s all!

For an even higher resolution image, or even larger fonts, you can continue to change the text and legend sizes accordingly. See guidance at (https://www.christophenicault.com/post/understand_size_dimension_ggplot2/).