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

3

8

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.

Lalittah answered 18/9, 2018 at 22:33 Comment(6)
showtext doesn't work well with RStudio graphic devices github.com/yixuan/showtext/issues/7Sandbag
Can you download "Pragati Narrow" font and try the extrafont package instead? stackoverflow.com/a/51888677Sandbag
@Sandbag 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.3Lalittah
it should work. Follow the steps in the answer that I linked above. I tested it before for both Windows and LinuxSandbag
@Sandbag Updated. I don't see a difference, but I've probably done something incorrect.Lalittah
see my answer for a working exampleSandbag
S
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

Sandbag answered 19/9, 2018 at 0:14 Comment(2)
Also relevant andrewheiss.com/blog/2017/09/27/…Sandbag
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...Lalittah
R
7

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.

Rambo answered 10/10, 2021 at 0:29 Comment(1)
FWIW this also worked for me.Emanation
I
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.

Impermeable answered 12/10, 2018 at 19:16 Comment(0)
S
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

Sandbag answered 19/9, 2018 at 0:14 Comment(2)
Also relevant andrewheiss.com/blog/2017/09/27/…Sandbag
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...Lalittah

© 2022 - 2024 — McMap. All rights reserved.