【问题标题】:ggsave() does not bolden text, and it changes font of all text instead of just plot titleggsave() 不会加粗文本,它会更改所有文本的字体,而不仅仅是绘图标题
【发布时间】:2019-02-23 00:22:26
【问题描述】:

我正在 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 的建议。我把谷歌字体下载到我的电脑上。这是我的新代码。

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 控制台中也没有看到任何错误或警告。

【问题讨论】:

  • showtext 不适用于 RStudio 图形设备 github.com/yixuan/showtext/issues/7
  • 您可以下载“Pragati Narrow”字体并尝试使用extrafont 包吗? stackoverflow.com/a/51888677
  • @Tung 谢谢。我确实注意到 RStudio 的查看器不适用于 showtext。但是下载“Pragmati Narrow”会改变ggsave() 的输出吗?我也不确定如何在我的计算机上安装该字体。我正在使用 Debian 9.3
  • 它应该可以工作。按照我上面链接的答案中的步骤进行操作。我之前在 Windows 和 Linux 上都测试过
  • 查看我的答案以获取工作示例

标签: r ggplot2 fonts showtext extrafont


【解决方案1】:

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

我可以通过使用installr::uninstall.packages("ragg") 卸载ragg 包来使其正常工作。

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

【讨论】:

    【解决方案2】:

    这里我也给出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默认使用“文泉易微黑”字库,以支持CJK字符。但是,该系列没有粗体字体,因此在您的原始代码中,轴标题以常规字体显示。我建议始终在基本图中设置par(family = "sans"),在ggplot 图中设置theme_grey(base_family = "sans")

    附带说明,这并不意味着showtext 不能在 RStudio 中使用。您可以调用x11() 或类似的方式打开一个窗口,showtext 应该可以很好地使用它。

    【讨论】:

      【解决方案3】:

      这适用于我的 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)
      

      【讨论】:

      • 当我用“Pragati Narrow”替换“BitstreamVeraSansMono”时,它工作正常。实际上,当我重新运行我在“更新”中发布的代码时,图表正确显示。我怀疑重新启动 RStudio 与此有关...
      猜你喜欢
      • 2015-04-21
      • 1970-01-01
      • 1970-01-01
      • 2014-04-24
      • 2015-07-31
      • 1970-01-01
      • 2020-07-08
      • 2020-12-22
      相关资源
      最近更新 更多