【问题标题】:Cannot import fonts into R无法将字体导入 R
【发布时间】:2022-12-30 02:40:12
【问题描述】:

我知道这个问题已经发布了,但我觉得我已经尝试了大部分解决方案而没有找到任何成功。

我正在使用 hrbrthemes 绘制我的 ggplot 图表,它一直告诉我没有导入正确的字体。

我在 Windows 上安装了所有字体,我使用了 extrafont 包并运行了 font_import() 命令。我不断收到以下错误:

C:\Windows\Fonts\RobotoCondensed-Bold.ttf : No FontName. Skipping.

对于绝对所有的字体。然而,如果我转到C:\Windows\Fonts,您可以从这张图片中看到RobotoCondensed 字体系列已正确安装。

loadfonts(device = "win") 对我也没有任何作用。我已经尝试了所有组合,包括在两者之间重新启动我的 r 会话,当我检查可用的字体时,我仍然得到这个:

这是我的会话信息:

> sessionInfo()
R version 4.1.3 (2022-03-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 22000)

Matrix products: default

locale:
[1] LC_COLLATE=English_Switzerland.1252  LC_CTYPE=English_Switzerland.1252    LC_MONETARY=English_Switzerland.1252 LC_NUMERIC=C                        
[5] LC_TIME=English_Switzerland.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] extrafont_0.17   hrbrthemes_0.8.0

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.7        pillar_1.7.0      compiler_4.1.3    later_1.3.0       tools_4.1.3       digest_0.6.29     evaluate_0.15     lifecycle_1.0.1  
 [9] tibble_3.1.6      gtable_0.3.0      pkgconfig_2.0.3   rlang_0.4.12      DBI_1.1.2         cli_3.1.0         rstudioapi_0.13   writexl_1.4.0    
[17] xfun_0.30         fastmap_1.1.0     Rttf2pt1_1.3.10   stringr_1.4.0     knitr_1.37        systemfonts_1.0.4 gdtools_0.2.4     generics_0.1.2   
[25] vctrs_0.3.8       grid_4.1.3        glue_1.6.0        R6_2.5.1          fansi_0.5.0       RMySQL_0.10.23    pool_0.1.6        rmarkdown_2.13   
[33] farver_2.1.0      ggplot2_3.3.5     purrr_0.3.4       extrafontdb_1.0   magrittr_2.0.1    scales_1.1.1      ggthemes_4.2.4    ellipsis_0.3.2   
[41] htmltools_0.5.2   assertthat_0.2.1  colorspace_2.0-2  labeling_0.4.2    utf8_1.2.2        stringi_1.7.6     munsell_0.5.0     crayon_1.5.0 

有没有人知道如何解决这个问题?

【问题讨论】:

  • 我现在在 Mac 上,所以我无法检查,但上次我在 Windows 中安装字体时,它为我的用户帐户将它们安装在不同的文件夹中。手动将字体文件移动到C:\Windows\Fonts 修复了它。
  • 你好,谢谢你的回答。问题是,我直接在C:\Windows\Fonts下查看,它们似乎都已正确安装。我只是无法以某种方式将它们放入 R 中。
  • 确保您确实查看了这些文件。我似乎记得当我查看字体文件夹时,窗口在其字体界面中显示字体,但是右键单击并执行“获取信息”或“显示详细信息”之类的操作,我可以看到字体文件实际上位于另一个位置.
  • 我刚刚检查了属性,它们确实在 C:\Windows\Fonts 文件夹中
  • 我有同样的问题,Windows 很难检查字体是否存在,或者它是否只是文件的链接。我最终在 R 中检查了 list.files,但 ttf 文件不存在。我在回答中添加了更多细节。

标签: r ggplot2 fonts extrafont hrbrthemes


【解决方案1】:

据我所知,最好的解决方案是showtextsysfonts。首先,将字体添加到会话中:

# directly from google fonts
sysfonts::font_add_google("Roboto Condensed")
# or add an arbitrary font with
sysfonts::font_add("Roboto Condensed", regular = "RobotoCondensed-Regular.ttf")

完成后,只需加载showtext并运行showtext_auto()一次即可激活它(您需要在每个会话中重复add_font*showtext_auto):

library(ggplot2)
library(showtext)

showtext_auto()
ggplot(mtcars, aes(mpg, wt)) +
  geom_point() +
  labs(x="Fuel efficiency (mpg)", y="Weight (tons)",
       title="Seminal ggplot2 scatterplot example",
       subtitle="A plot that is only useful for demonstration purposes",
       caption="Brought to you by the letter 'g'") + 
  hrbrthemes::theme_ipsum_rc()

reprex package (v2.0.1) 创建于 2022-03-22

您也可以尝试使字体永久可用。但它似乎受到打击和错过。理论上,您需要将字体安装到C:WindowsFonts,您可以通过 1) 解压缩字体,2) 右键单击​​并“为所有用户安装”来完成。如果您以不同的方式安装字体,Windows 很可能只会将链接放入C:WindowsFonts,这是 R 无法处理的。

您可以使用以下方法检查可用字体:

fonts_df <- sysfonts::font_files()
View(fonts_df)

根据我的经验,这适用于大多数字体,但对于 Roboto,由于某种原因我仍然没有运气。 add_font* 似乎是目前最好的方式。

【讨论】:

  • 谢谢!这行得通!很高兴在尝试让它工作几个小时后得到结果 :) 感谢您提供解决方法!
  • 我认为 showtext 不仅仅是一种解决方法,因为它解决了 base R 在字体方面的许多问题。我希望这些修复最终包含在 base R 中。
  • 你能用它来拉取hrbrthemes包中的所有字体吗?同意,我并不想淡化解决方案,我只是觉得不幸的是,考虑到 R 的年龄和字体的使用,它似乎仍然会引起如此多的恶化。
  • 我同意。我只用过 Roboto,但我认为可以使用相同的方法提取包中的所有字体。
  • 好的,如果我需要更多字体,我会记住这一点,再次感谢!
【解决方案2】:

我有同样的问题,这真的很令人沮丧 正如 JBGRuber 上面所建议的那样,Windows 似乎重命名了字体文件,但是当我使用

fonts_df <- sysfonts::font_files()
View(fonts_df)

他们都出现了。

无论如何,我一直在玩这个的方式是:

windowsFonts(Robot=windowsFont("Roboto Condensed")) # Assigning font to object

ggplot(mtcars, aes(mpg, wt)) +
  geom_point() +
  labs(x="Fuel efficiency (mpg)", y="Weight (tons)",
       title="Seminal ggplot2 scatterplot example",
       subtitle="A plot that is only useful for demonstration purposes",
       caption="Brought to you by the letter 'g'") +
  
  theme_bw() +
  theme(text=element_text(family="Robot",    
                          face="bold", 
                          size=12))

希望这可以帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-16
    • 2015-02-10
    • 2021-12-12
    • 2018-11-26
    • 1970-01-01
    • 2021-12-21
    相关资源
    最近更新 更多