r - knitr、开罗和 ggplot2 : font family 'Source Code Pro' not found in PostScript fontdatabase

标签 r fonts ggplot2 knitr cairo

尝试解决我拥有的 otf 字体问题(请参阅 How to use otf-font in ggplot2 graphics? ),我发现我可以在 ggplot2 中使用这些字体。

将knitr与Rmd文件一起使用是可以的:

---
title: "FontTest"
author: "me"
date: "22. April 2015"
output: html_document
---

```{r, echo=FALSE}
library(ggplot2)

ggplot(mtcars, aes(x=wt, y=mpg, color=factor(gear))) + geom_point() +
  ggtitle("Fuel Efficiency of 32 Cars") +
  xlab("Weight (x1000 lb)") + ylab("Miles per Gallon") +
  theme(text=element_text(size=16, family="Arial"))
```

但是当我想使用 LaTeX(Rnw 文件)时,我收到错误:

\documentclass{article}

\begin{document}
\SweaveOpts{concordance=TRUE}

<<>>=
Sys.setenv(LANG = "en")
library(ggplot2, quietly=TRUE)
@



<<>>=
ggplot(mtcars, aes(x=wt, y=mpg, color=factor(gear))) + geom_point() +
  ggtitle("Fuel Efficiency of 32 Cars") +
  xlab("Weight (x1000 lb)") + ylab("Miles per Gallon") +
  theme(text=element_text(size=16, family="Arial"))
@

\end{document}

错误如下:

Writing to file test.tex
Processing code chunks with options ...
 1 : echo keep.source term verbatim (test.Rnw:6)
 2 : echo keep.source term verbatim (test.Rnw:13)
Error in grid.Call.graphics(L_text, as.graphicsAnnot(x$label), x$x, x$y,  : 
  invalid font type
Calls: <Anonymous> ... drawDetails -> drawDetails.text -> grid.Call.graphics
In addition: There were 50 or more warnings (use warnings() to see the first 50)
Execution halted

我使用的是 Mac OS X Mavericks。

更新

我找到了使用开罗的解决方法:

\documentclass{article}

\begin{document}



<<>>=
  Sys.setenv(LANG = "en")
library(ggplot2, quietly=TRUE)
library(Cairo)
@



<<dev='cairo_pdf'>>=

  ggplot(mtcars, aes(x=wt, y=mpg, color=factor(gear))) + geom_point() +
  ggtitle("Fuel Efficiency of 32 Cars") +
  xlab("Weight (x1000 lb)") + ylab("Miles per Gallon") +
  theme(text=element_text(size=16, family="Source Code Pro"))

@

\end{document}

但现在我收到很多警告:

## Warning in grid.Call(LtextBounds, as.graphicsAnnot(x$label), x$x,x$y, :  font family 'Source Code Pro' not found in PostScript fontdatabase

我想避免抑制这些警告。那么我该怎么做才能消除这些警告呢?

最佳答案

我找到了以下解决方案:

我用

\usepackage{tikz}

<<plot, dev='tikz'>>

在我的 .Rnw 文件中。通过这种方式,我可以在绘图中获得 LaTeX 项目的字体。

关于r - knitr、开罗和 ggplot2 : font family 'Source Code Pro' not found in PostScript fontdatabase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29803524/

相关文章:

r - ggplot2,x轴无法准确显示刻度

r - 在 R 中使用 apply 和assign

R:根据另一列中的值计算一列中值的数量

r - 如何将geom_bar、geom_point、geom_line合并为一张图?

r - 具有平滑置信区间的 ggplot2 误差条

r - 使用 xtable 函数更改从 lm 回归中获得的表的行名称

javascript - 用 puppeteer 创建的 PDF 不显示 Font Awesome 图标

android - 为 ListView 设置字体

css - 字体系列不适用于字体

r - ggplot颜色按因子水平划分,有些颜色非常相似,无法区分某些组的点