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

问题描述 投票:1回答:2

我在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()代码时。

enter image description here

这是ggsave()产生的。

enter image description here

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

更新:我试过董的建议。我将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
)

似乎没有改变任何东西。

enter image description here

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

r ggplot2 fonts showtext extrafont
2个回答
1
投票

这适用于我的Linux Mint Rosa机器。您需要按照此extrafont下载并导入所需的字体到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
投票

在这里我也给出了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"
        )
    )

enter image description here

长版本:当ggplot中未指定基类时,showtext使用“WenQuanYi MicroHei”字体系列作为默认值,以支持CJK字符。但是,此系列没有粗体字体,因此在原始代码中,轴标题以常规字体显示。我建议总是在par(family = "sans")地块中设置theme_grey(base_family = "sans")基础地块和ggplot

作为旁注,这并不意味着showtext不能在RStudio中使用。你可以打电话给x11()或类似的打开一个窗口,showtext应该可以正常使用它。

© www.soinside.com 2019 - 2024. All rights reserved.