简体   繁体   English

无法将 fonts 导入 R

[英]Cannot import fonts into R

I'm aware this issue has been posted on already, but I feel like I've tried most of the solutions without finding any success.我知道这个问题已经发布了,但我觉得我已经尝试了大部分解决方案而没有找到任何成功。

I'm using hrbrthemes to plot my ggplot graphs, and it keeps telling me I don't have the right fonts imported.我将hrbrthemes用于 plot 我的ggplot图,它一直告诉我我没有导入正确的 fonts。

I installed all the fonts on windows, I used the extrafont packages and ran the font_import() command.我在 windows 上安装了所有 fonts,我使用了extrafont包并运行了font_import()命令。 I keep getting the following errors:我不断收到以下错误:

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

For absolutely all the fonts. Yet if I go to C:\Windows\Fonts , you can see from this image that the RobotoCondensed font family is properly installed.对于所有 fonts。但是,如果我将 go 更改为C:\Windows\Fonts ,您可以从此图像中看到RobotoCondensed字体系列已正确安装。

在此处输入图像描述

The loadfonts(device = "win") also doesn't do anything for me. loadfonts(device = "win")对我也没有任何作用。 I've tried all combinations including restarting my r session in between things and I still get this when I check what fonts are available:我已经尝试了所有组合,包括在两者之间重新启动我的 r session,当我检查 fonts 可用时我仍然得到这个:

在此处输入图像描述

Here's my session info:这是我的 session 信息:

> 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 

Does anyone have an idea on how to fix this issue?有没有人知道如何解决这个问题?

The best solution to my knowledge is showtext and sysfonts .据我所知,最好的解决方案是showtextsysfonts First, add the font to the session:首先,将字体添加到session:

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

Once this is done, simply load showtext and run showtext_auto() once to activate it (you need to repeat add_font* and showtext_auto every session):完成后,只需加载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()

Created on 2022-03-22 by the reprex package (v2.0.1)reprex package (v2.0.1) 创建于 2022-03-22

You can also try to make the font available permanently.您也可以尝试使字体永久可用。 But it seems to be hit and miss.但它似乎受到打击和错过。 Theoretically, you need to install the font into C:\Windows\Fonts , which you can do by 1) unzip the fonts, 2) right click and "install for all users".理论上,您需要将字体安装到C:\Windows\Fonts中,您可以通过 1) 解压缩 fonts,2) 右键单击并“为所有用户安装”。 If you install the fonts in a different way, there is a good chance Windows will only put a link into C:\Windows\Fonts , which R can't deal with.如果您以不同的方式安装 fonts,很有可能 Windows 只会将链接放入C:\Windows\Fonts ,R 无法处理。

You can check available fonts with:您可以通过以下方式检查可用的 fonts:

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

This works for most fonts in my experience but with Roboto, I still had no luck for some reason.根据我的经验,这对大多数 fonts 都有效,但对于 Roboto,由于某种原因我仍然没有运气。 add_font* seems to be the best way to go for now. add_font*似乎是目前 go 的最佳方式。

I have the same issue, and it is really frustrating As JBGRuber suggested above, it seems that windows renames the font files, but when I use我有同样的问题,这真的很令人沮丧正如上面 JBGRuber 所建议的,似乎 windows 重命名了字体文件,但是当我使用

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

They all show up.他们都出现了。

At any rate, the way I have been playing around this is:无论如何,我一直在玩这个的方式是:

windowsFonts(Robot=windowsFont("Roboto Condensed"))

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

在此处输入图像描述

Hope this helps希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM