ggsave() does not bolden text, and it changes font of all text instead of just plot title

5.2k views Asked by At

I'm making a chart in ggplot2 and ggsave() does not do what I expect.

require(ggplot2)
require(showtext)

showtext_auto()
hedFont <- "Pragati Narrow"
font_add_google(
  name = hedFont,
  family = hedFont,
  regular.wt = 400,
  bold.wt = 700
)

chart <- ggplot(
  data = cars,
  aes(
    x = speed,
    y = dist
  )
) +
  geom_point() +
  labs(
    title = "Here is a title",
    subtitle = "Subtitle here"
  ) +
  theme(
    plot.title = element_text(
      size = 20,
      family = hedFont,
      face = "bold"
    ),
    axis.title = element_text(
      face = "bold"
    )
  )

ggsave(
  filename = "myplot",
  plot = chart,
  device = "png",
  path = "~/Desktop",
  width = 300,
  height = 200,
  units = "mm",
  dpi = 72
)

What I expected was for the chart's title to have the custom font. Instead, ggsave() makes a chart where all the text has the font. I expected the axis titles to be bold, but they are not.

Here's what I see in RStudio viewer when I run the ggplot() code in it.

enter image description here

Here's what ggsave() produces.

enter image description here

I want ggsave() to make a chart where only the chart's title has the font and the axes' titles are bold.

UPDATE: I tried Tung's suggestion. I downloaded the Google Font onto my computer. Here's my new code.

font_import(
  paths = "/usr/share/fonts/truetype/google-fonts/",
  recursive = T,
  prompt = F,
  pattern = "Pragati"
)
loadfonts(device = "pdf")
loadfonts(device = "postscript")

myFont <- "Pragati Narrow"

chart <- ggplot(
  data = cars,
  aes(
    x = speed,
    y = dist
  )
) +
  geom_point() +
  labs(
    title = "Here is a title",
    subtitle = "Subtitle here"
  ) +
  theme(
    plot.title = element_text(
      size = 20,
      family = myFont,
      face = "bold"
    ),
    axis.title = element_text(
      face = "bold"
    )
  )

ggsave(
  filename = "myplot2.png",
  plot = chart,
  device = "png",
  path = "~/Desktop",
  width = 300,
  height = 200,
  units = "mm",
  dpi = 72
)

Doesn't seem to have changed anything.

enter image description here

I don't see any errors or warnings in the RStudio console either.

3

There are 3 answers

2
Tung On BEST ANSWER

This worked on my Linux Mint Rosa machine. You need to download and import the desired font to extrafont database per this answer

library(extrafont)
library(ggplot2)

hedFont <- "BitstreamVeraSansMono"

chart <- ggplot(
  data = cars,
  aes(
    x = speed,
    y = dist
  )
) +
  geom_point() +
  labs(
    title = "Here is a title",
    subtitle = "Subtitle here"
  ) +
  theme(
    plot.title = element_text(
      size = 20,
      family = hedFont,
      face = "bold"
    ),
    axis.title = element_text(
      face = "bold"
    )
  )
chart

ggsave(
  filename = "./output/myplot.png",
  plot = chart,
  type = "cairo",
  height = 4,
  width = 6,
  dpi = 150)

enter image description here

1
PupHendo On

I had a similar problem using the extrafont package, where the font I specified would show in the RStudio viewer but change when I saved as a .png with ggsave(). Neither of the above answers worked for me (font was already saved in my extrafont database and specifying the base_family didn't work).

I was able to get it working simply by uninstalling the ragg package using installr::uninstall.packages("ragg").

I don't know why this works, if anyone has any explanations for this I'd be interested to hear.

0
yixuan On

Here I also give the showtext solution.

Short version: add theme_grey(base_family = "sans") to the ggplot statement, and below is the output as expected.

chart <- ggplot(data = cars, aes(x = speed, y = dist)) +
    geom_point() +
    labs(title = "Here is a title", subtitle = "Subtitle here") +
    theme_grey(base_family = "sans") +
    theme(
        plot.title = element_text(
            size = 20,
            family = hedFont,
            face = "bold"
        ),
        axis.title = element_text(
            face = "bold"
        )
    )

enter image description here

Long version: when the base family is not specified in ggplot, showtext uses the "WenQuanYi MicroHei" font family as the default, in order to support CJK characters. However, this family has no bold font face, so in your original code the axis title was displayed in a regular font face. I would suggest always setting par(family = "sans") in base plots and theme_grey(base_family = "sans") in ggplot plots.

As a side note, it does not mean that showtext cannot be used inside RStudio. You can call x11() or the alike to open a window and showtext should work well with it.