简体   繁体   English

自定义字体在 ggplot Rstudio 中显示正确,但在使用 R Markdown 编织时不正确

[英]Custom font appear correct in ggplot Rstudio but not when knit with R Markdown

I am using the font Lato ( https://fonts.google.com/specimen/Lato ) as part of a package and loading it on windows machines using windowsFonts( Lato = windowsFont("Lato")).我使用字体 Lato ( https://fonts.google.com/specimen/Lato ) 作为包的一部分,并使用 windowsFonts( Lato = windowsFont("Lato")) 将其加载到 Windows 机器上。 This font works well when producing jpg, png, pdf graphics on both Windows and Mac.此字体在 Windows 和 Mac 上生成 jpg、png、pdf 图形时效果很好。 I am not using extrafont or showtext due to inconsistencies with graphics on Windows and Macs and other error we encountered.由于 Windows 和 Mac 上的图形不一致以及我们遇到的其他错误,我没有使用 extrafont 或 showtext。

Here is a comparison of default font and Lato.这是默认字体和 Lato 的比较。 The difference can easily be seen in the dot above the "i", which is a circle in Lato but a square in the default font.在“i”上方的点中可以很容易地看出差异,它在 Lato 中是一个圆圈,但在默认字体中是一个正方形。 This was produced in Rstudio by simply running the chunk and pasting the graphic.这是在 Rstudio 中通过简单地运行块并粘贴图形来生成的。

在此处输入图像描述

However, when knitting on Windows (not tested yet on Mac), the Lato font does not work.但是,在 Windows 上编织时(尚未在 Mac 上测试),Lato 字体不起作用。 Here is the example and the common warning message这是示例和常见警告消息

Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, : font family 'Lato' not found, will use 'sans' instead

在此处输入图像描述

Here is a fully reproducible example, assuming Lato is installed.这是一个完全可重现的示例,假设安装了 Lato。

---
title: "Untitled"
output: html_document
date: '2022-07-18'
editor_options: 
chunk_output_type: console
---
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(patchwork)
library(extrafont)
# check if you have the fonts installed
get_font_path <- extrafont:::ttf_find_default_path()
  fonts_list <- list.files(get_font_path)
  if(
  all(c("Lato-Regular.ttf" %in% fonts_list,
    "Lato-Bold.ttf" %in% fonts_list,
    "Lato-Italic.ttf" %in% fonts_list)) == FALSE){
    message("Please install Lato here: https://fonts.google.com/specimen/Lato")
  }
  
# for windows machines, load font
windowsFonts(`Lato` = windowsFont("Lato"))
windowsFonts() #Lato will apppear
default_example <- mtcars %>%
  slice(1:10) %>%
  rownames_to_column("mfg") %>%
  ggplot(aes(x=mpg, y=reorder(mfg, mpg), fill=as.factor(cyl)))+
  geom_col()+
  ggtitle("No theme_amplify()")+
  ylab("Manufacturer")+
  theme(legend.position = "none",
        plot.title=element_text(size=14))

font_example <- mtcars %>%
  slice(1:10) %>%
  rownames_to_column("mfg") %>%
  ggplot(aes(x=mpg, y=reorder(mfg, mpg), fill=as.factor(cyl)))+
  geom_col()+
  ggtitle("theme_amplify()")+
  ylab("Manufacturer")+
  theme_minimal(base_family="Lato")+
    theme(legend.position = "none",
        plot.title=element_text(size=14))

default_example + font_example
# in the default font, the dot over the i is square
# with Lato, the dot over the i is a circle

Note that the above code works with a simple base R plot:请注意,上面的代码适用于简单的基本 R 图:

plot(1:10)
text(5, 2, "Hi", family = "Lato", cex=3)
text(7, 2, "Hi", font = 2, cex=3)

在此处输入图像描述

Update 1: I have confirmed that this is not an issue on Mac, only Windows.更新 1:我已经确认这不是 Mac 上的问题,只有 Windows。

Update 2: When calling dev.list() in R Studio, I get RStudioGD;更新 2:在 R Studio 中调用dev.list()时,我得到 RStudioGD; however, in R Markdown, only png shows up.但是,在 R Markdown 中,只显示 png。 Could be related?可能有关系吗?

Using your code, the font, Lato, rendered correctly.使用您的代码,字体 Lato 正确呈现。 However, I'm on Mac.但是,我在 Mac 上。 (I didn't use the code that was strictly for Windows, obviously.) Since I'm not able to reproduce your error with the extrafonts package, I thought I would provide an alternative method to control the font. (显然,我没有使用严格用于 Windows 的代码。)由于我无法使用extrafonts包重现您的错误,我想我会提供一种替代方法来控制字体。

You can use the showtext package to do this.您可以使用showtext包来执行此操作。 (You can download new fonts from Google with this package, as well.) (您也可以使用此软件包从 Google 下载新字体。)

Whether it's on your computer or not, you can call any Google font family like this.无论是否在您的计算机上,您都可以像这样调用任何 Google 字体系列。

font_add_google("Lato", "Lato") # family & ref name 

This is font_all_google(<font family name>, <name you ref to use this font>) .这是font_all_google(<font family name>, <name you ref to use this font>) You can make the reference name what you like (it doesn't have to be the font name).您可以将参考名称设为您喜欢的名称(不必是字体名称)。 If, for any reason, the call for the font does not work, you will see a message from the command font_add_google .如果出于任何原因,对字体的调用不起作用,您将看到来自命令font_add_google的消息。 Think of it as 'silence is consent.'把它想象成“沉默就是同意”。

You have to tell it to start listening, too.你也必须告诉它开始听。

showtext_auto() # listen!

I have it set up like this.我是这样设置的。

---
title: "Untitled"
output: html_document
date: '2022-07-18'
editor_options: 
chunk_output_type: console
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(patchwork)
library(showtext)
font_add_google("Lato", "lato")
showtext_auto()
```

Since Lato is extremely similar to the base font, I made the titles a bit bigger and italicized.由于 Lato 与基本字体极为相似,因此我将标题稍大一点并用斜体表示。 In Lato, the print a has a hat, like an 'a' in the font in this text in Stack Overflow.在 Lato 中,打印 a 有一个帽子,就像 Stack Overflow 中此文本中字体中的“a”。 However, when you italicize Lato, an 'a' is like the o with a tail.但是,当您使用斜体 Lato 时,“a”就像带尾巴的 o。 Check it out.一探究竟。 Oh, and in the base R plot, I wrote it out a bit more (to add an 'a').哦,在基本的 R 图中,我把它写得更多(添加一个“a”)。 I italicized the Lato text, too.我也将 Lato 文本斜体。

If my terrible description of hats and tails doesn't make sense, then check the differences in the 'a' in this image I took from the Lato Google font page.如果我对帽子和尾巴的糟糕描述没有意义,请检查我从 Lato Google 字体页面获取的这张图片中“a”的差异。

在此处输入图像描述

```{r more,echo=F}
default_example <- mtcars %>%
  slice(1:10) %>%
  rownames_to_column("mfg") %>%
  ggplot(aes(x=mpg, y=reorder(mfg, mpg), fill=as.factor(cyl)))+
  geom_col()+
  ggtitle("No theme_amplify()")+
  ylab("Manufacturer")+
  theme(legend.position = "none",
        plot.title=element_text(size = 20, face = "italic"))

default_example
```

```{r another, echo=F}
mtcars %>%
  slice(1:10) %>%
  rownames_to_column("mfg") %>%
  ggplot(aes(x=mpg, y=reorder(mfg, mpg), fill=as.factor(cyl)))+
  geom_col()+
  ggtitle("theme_amplify()")+
  ylab("Manufacturer")+
  theme_minimal(base_family="Lato")+
    theme(legend.position = "none",
          plot.title=element_text(size = 20, face = "italic"))
```

```{r uhhuh, echo=F}
plot(1:10)
text(5, 5, "Words with an a", font = 3, cex=3, family = "Lato")
text(7, 2, "Words with an a", font = 1, cex=3)
```

在此处输入图像描述

在此处输入图像描述

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

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