Font gets really small when saving to png using ggsave() and showtext

Previously, I usually save plots as both png and pdf using ggsave(), and I'm used to them turning out identical. However recently my png-plot looks really strange, the font gets very small compared to the pdf. I have found previous posts here that seem to have the exact same problem, see Font size changes when saving in 'png' files, not in 'pdf' or 'svg' and ggsave png super small text.

I noticed that this only happens when I use the package showtext, without necessarily changing the font, just calling showtext_auto(). But again, I have used showtext before without this issue (and it looks like only one of the above linked examples uses showtext).

I have tried specifying width and height, dpi, and units to ggsave(), and none of that seems to help. I'm confused, as this has worked just fine previously. I'm using R version 4.2.0.

The following code produces two different plots for me:

library(ggplot2)
library(showtext)

showtext_auto()

df <- data.frame(x = runif(10), y = runif(10))

p <- ggplot(df, aes(x = x, y = y)) +
  geom_point() +
  labs(title = "The title of the plot",
       subtitle = "This plot illustrates how the font gets all small \nwhen saving to png")

p
ggsave("font_size_issue.png")
ggsave("font_size_issue.pdf")

This is the png:

However, if I instead save it like this, the png looks like I want it to (if I specify the same width and height it looks identical to the pdf):

png("font_size_issue_fixed.png", width = 5.75, height = 4.75, res = 300, units = "in")
print(p)
dev.off()

So doing it like this sort of solves my problem, but I would really prefer to use ggsave()! Any ideas as to what may cause this and how to save an identical plot using ggsave()?

hi dear

you can increase the font to the size you need by adding :

theme(axis.text = element_text(size = 20))

p <- ggplot(df, aes(x = x, y = y)) +
geom_point() +
labs(title = "The title of the plot",
subtitle = "This plot illustrates how the font gets all small \nwhen saving to png")+theme(axis.text = element_text(size = 20))

Kind Regards

I'm aware of that, but the issue here is that the text is still relatively smaller in the png version of the plot compared to the pdf version of the plot (even after specifying text size).

1 Like

I am seeing the same issue with microscopic text in PNG output, and given the two other posts linked by @EmmaSkarstein in the original post above, clearly this isn't an isolated problem.

I can confirm that it seems to be related to the use of showtext, as running showtext_auto(enable = FALSE) immediately prior to the call to ggsave() results in the text being the expected size (i.e. the same as in PDF output), rather than being well-nigh invisible, but is no longer the specified font (i.e. perhaps just uses the default face).

Using showtext itself is still needed to avoid this bug when using a non-standard named font:

Error in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y,  : 
  invalid font type
Calls: <Anonymous> ... drawDetails -> drawDetails.text -> grid.Call.graphics

but switching it off selectively before exporting to PNG via ggsave() seems to be OK.

The invalid point type errors only started arising for me after an upgrade to macOS Monterey 12.5.x. Monterey does seem to shift fonts around and lock down some of the system-level fonts.

1 Like

Try something like this:
ggsave("Rplot.png", width = 5000, height = 3500, units = "px", dpi = 800)

You can edit width, height and then adjust dpi to increase font and geom size.

Thanks, but the OP understands all that. The issue is that ggsave() doesn't work in conjunction with showtext when saving to PNG: the scaling is not consistent when using exactly the same settings to save to PDF. This suggestion doesn't help, as the issue is the relative size of the text to the overall image, not the overall dimensions of the image itself (which are unaffected by the bug).

2 Likes

according to using showtext leads to smaller font size in ggplot2 · Issue #51 · yixuan/showtext (github.com)
the recommended solution seems to be to set the show_text_opts(dpi=300) # or to a value that achieves your goal. I tested that on this setup example, and got the png looking like the pdf

library(ggplot2)
library(showtext)

showtext_auto()
showtext_opts(dpi = 300)

df <- data.frame(x = runif(10), y = runif(10))

p <- ggplot(df, aes(x = x, y = y)) +
  geom_point() +
  labs(title = "The title of the plot",
       subtitle = "This plot illustrates how the font gets all small \nwhen saving to png")

p

ggsave("font_size_issue.png")
ggsave("font_size_issue.pdf")
4 Likes

Wonderful, this solves it for me! Thank you so much, my searching hadn't uncovered that one.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.