5

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.

Tung
  • 26,371
  • 7
  • 91
  • 115
Username
  • 3,463
  • 11
  • 68
  • 111
  • 1
    `showtext` doesn't work well with RStudio graphic devices https://github.com/yixuan/showtext/issues/7 – Tung Sep 18 '18 at 22:58
  • 1
    Can you download "Pragati Narrow" font and try the `extrafont` package instead? https://stackoverflow.com/a/51888677/ – Tung Sep 18 '18 at 22:59
  • @Tung Thanks. I did notice RStudio's viewer doesn't work well with `showtext`. But would downloading "Pragmati Narrow" change the output of `ggsave()`? I'm also not sure how to install that font on my computer. I'm using Debian 9.3 – Username Sep 18 '18 at 23:22
  • 1
    it should work. Follow the steps in the answer that I linked above. I tested it before for both Windows and Linux – Tung Sep 18 '18 at 23:24
  • @Tung Updated. I don't see a difference, but I've probably done something incorrect. – Username Sep 18 '18 at 23:43
  • 1
    see my answer for a working example – Tung Sep 19 '18 at 00:14

3 Answers3

6

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.

PupHendo
  • 145
  • 1
  • 8
2

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.

yixuan
  • 774
  • 3
  • 6
1

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

Tung
  • 26,371
  • 7
  • 91
  • 115
  • 1
    Also relevant https://www.andrewheiss.com/blog/2017/09/27/working-with-r-cairo-graphics-custom-fonts-and-ggplot/ – Tung Sep 19 '18 at 00:31
  • 1
    When I replace "BitstreamVeraSansMono" with "Pragati Narrow", it works fine. Actually, when I reran the code I posted in the "update," the charts came out correctly. I suspect restarting RStudio had something to do with this... – Username Sep 19 '18 at 00:55