ggsave() 不会加粗文本,它会更改所有文本的字体而不仅仅是绘图标题

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

我正在用 ggplot2 制作图表,但 ggsave() 没有达到我的预期。

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
)

我希望图表标题具有自定义字体。相反,ggsave() 制作了一个图表,其中所有文本都具有该字体。我希望轴标题是粗体,但它们不是。

这是我在 RStudio 查看器中看到的 运行 ggplot() 代码。

这是 ggsave() 产生的结果。

我想要 ggsave() 制作一个图表,其中只有图表的标题有字体,轴的标题是粗体。

更新:我尝试了 Tung 的建议。我将 Google 字体下载到我的电脑上。这是我的新代码。

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
)

似乎没有任何改变。

我在 RStudio 控制台中也没有看到任何错误或警告。

这适用于我的 Linux Mint Rosa 机器。您需要根据此 answer

下载所需字体并将其导入 extrafont 数据库
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)

这里我也给出showtext的解决方法

简短版:将theme_grey(base_family = "sans")添加到ggplot语句,下面是预期的输出。

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"
        )
    )

长版:当ggplot中未指定基本字体系列时,showtext默认使用"WenQuanYi MicroHei"字体系列,为了支持 CJK 字符。但是,这个系列没有粗体字体,因此在您的原始代码中,轴标题以常规字体显示。我建议始终在基本图中设置 par(family = "sans"),在 ggplot 图中设置 theme_grey(base_family = "sans")

作为旁注,这并不意味着 showtext 不能在 RStudio 中使用。您可以调用 x11() 或类似的方法来打开 window 并且 showtext 应该可以很好地使用它。

我在使用 extrafont 包时遇到了类似的问题,我指定的字体会显示在 RStudio 查看器中,但当我另存为 .pngggsave() 时会发生变化。以上答案都不适合我(字体已经保存在我的 extrafont 数据库中并且指定 base_family 无效)。

我可以通过使用 installr::uninstall.packages("ragg").

卸载 ragg 包来让它正常工作

我不知道为什么会这样,如果有人对此有任何解释,我很想听听。