3

我正在努力在 macOS 上的 ggplot 中应用字体,需要一些帮助。希望这对其他人也有帮助,因为我进行了很多搜索并尝试了各种无济于事。似乎存在的字体无法识别,然后出现奇怪的间距(这可能是相关的)。在这个例子中,我使用的是 BoB Rudis 的 FT 主题,它使用了 Robot Condensed Google 字体。

在 Mac 上使用该字体有一些报告的问题,但这些似乎与 using hrbrthemes::import_roboto_condensed()function (似乎只是部分下载字体)有关,而不是 via sysfonts::font_add_google,这就是我正在做的。

当我运行附加代码时,前 46 个警告是

In grid.Call.graphics(C_text, as.graphicsAnnot(x$label),  ... :
font family 'Roboto Condensed Light' not found, will use 'sans' instead

即使,如果我运行 fonts(),它也会返回“Roboto Condensed”和“Roboto Condensed Light”。这些字体已完全安装在我的机器上,并可在其他应用程序中使用。有趣的是,在查看了整个 FT 主题定义后,我看不到任何对“Roboto Condensed Light”的直接引用。

如果我将 bodyFont 更改为“Times”,即使也安装了它,我也会收到类似的警告。更奇怪的是,一些文字开始出现在显然是时代的东西上。

我确实尝试showtext::showtext_auto()过,因为这似乎使缩放更具可预测性(请参见此处),但是这样一来,似乎根本无法识别任何字体,就好像它有一个单独的字体数据库一样。

所以我的问题是:

  1. 我是否按照正确的步骤使用特定字体?
  2. 在 Mac 上输出到 PNG 等时,extrafont::loadfonts() 有什么关系(所以不是 PDF 或使用 Windows)
  3. 当它说未找到字体时,它正在查看哪个字体数据库?
  4. 是否showtext使用不同的字体数据库?

我在运行 Mojave 的英特尔 MacBook Pro 上。

install.packages("extrafontdb")  # reset extrafont fonttable
devtools::install_github("hrbrmstr/hrbrthemes", force = TRUE)

# Run if you get Segmentation faults. This can happen if you install extrafont from source (1.0.3) which seems to corrupt Rttf2pt1
# library(remotes)
# remotes::install_version("Rttf2pt1", version = "1.3.8")

# Load libraries
pacman::p_load(tidyverse, sysfonts, extrafont, ggforce, ggtext, showtext, cowplot)
library(hrbrthemes)

# Install roboto via font_add_google instead as this import function only installs 3 font files and seems to cause issues as per
# https://github.com/hrbrmstr/hrbrthemes/issues/18#issuecomment-633253069
# hrbrthemes::import_roboto_condensed()

# Download Google Fonts into 'sysfonts'
headingFont <- "Roboto Condensed"
bodyFont <- "Open Sans"
sysfonts::font_add_google(headingFont)
sysfonts::font_add_google(bodyFont) 
bodyFont <- "Times" # Try Non Google Font

# Not fully sure what showtext does and whether it uses the same font definition, but seems to be useful in 
# setting the scaling correctly as per https://github.com/yixuan/showtext
showtext::showtext_opts(dpi = 300)
# Currently disabled as not even Times works with this
showtext::showtext_auto(enable = FALSE)

# Load fonts into extrafontdb font library - only needed when you've installed new fonts on the device (i.e. outside R))
#extrafont::font_import()

# Documentation suggests you need this to register the fonts with the (PDF) output device.  
# There is an option for device = "win".  Not clear what is needed for png output etc
  extrafont::loadfonts()

# Create Data
x <- runif(n = 20, min = 0, max = 9)
y <- runif(n = 20, min = 0, max = 9)
xend <- runif(n = 20, min = 0, max = 9)
yend <- runif(n = 20, min = 0, max = 9)
data = data.frame(x, xend, y, yend)

# Create Plot
p <- ggplot(data) +
  ggforce::geom_link(aes(x = x, xend = xend, y = y,  yend = yend, colour = "orange")) +
  hrbrthemes::theme_ft_rc(base_family = bodyFont) +
  # Allow styling of headings
  theme(
    plot.title=ggtext::element_markdown(size = 32, margin = margin(0, 0, 0.5, 0, "cm"), family = headingFont),
    plot.subtitle=ggtext::element_markdown(size = 14, margin = margin(0, 0, 0.4 , 0, "cm")),
    plot.caption=ggtext::element_markdown(size = 8)
  ) + 
  # Headings
  labs(title = "Main Plot Title which should be in more stylised font",
       subtitle = "Subtitle which should be in simpler font along with everything else on the plot",
       caption = "7.10.2021  |  Visualisation by @ChrisWoodsSays  |  Random Data") 

cowplot::ggdraw(p) +
  cowplot::draw_label(label = "First Label", x=0.168, y=0.426, color="white", size=10, fontfamily = bodyFont) +
  cowplot::draw_label(label = "Label after first", x=0.465, y=0.560, color="white", size=10, fontfamily = bodyFont) 

ggsave("SO.png", height = 18, width = 32, units = "cm")

ggplot 字体和间距问题

4

0 回答 0