r - knitr:无法创建带有 utf-8 字符的图形

标签 r encoding utf-8 knitr

以下是我的 .Rnw 文件:

\documentclass{article}
\begin{document}

<<myChunk>>=
options(warn = 2)
library(ggplot2)
library(directlabels)
data(BodyWeight,package="nlme")
BodyWeight$temp <- as.character(BodyWeight$Rat)
BodyWeight$temp[BodyWeight$temp == "4"] <- "HI₂"
p <- qplot(Time,weight,data=BodyWeight,colour=temp,geom="line")
direct.label(p,"first.qp")
@

\end{document}

以下是我如何从 R 调用 knitr:

library(knitr)
# I have tryied this but doesn't make difference:
# pdf.options(encoding='ISOLatin2.enc')
knit("mwe_knitr.Rnw")

我得到以下输出:

> knit("mwe_knitr.Rnw")


processing file: mwe_knitr.Rnw
  |......................                                           |  33%
  ordinary text without R code

  |...........................................                      |  67%
label: myChunk
Quitting from lines 5-13 (mwe_knitr.Rnw) 
Error in grid.Call(L_convert, x, as.integer(whatfrom), as.integer(whatto),  : 
  (converted from warning) conversion failure on 'HI₂' in 'mbcsToSbcs': dot substituted for <e2>

我尝试了编码解决方案,例如此处发布的: Rhtml: Warning: conversion failure on '<var>' in 'mbcsToSbcs': dot substituted for <var>

(我在上面的评论中准确地指出了我尝试解决该问题的位置)但它似乎并没有为我带来任何改变。

我在 Ubuntu 上使用 R 3.3.1 和 knitr 软件包 1.13。

最佳答案

看起来使用 cairo_pdf 设备可以解决此问题。在下面的 setup block 中,我将设备选项设置为 cairo_pdf 设备(即以 option(device = ...) 开头的行)全局 block 选项 dev 默认为“cairo_pdf”(在以 knitr::opts_chunk$set(... 开头的行中)。这种方法在knitr documentation(请参阅多字节字符编码部分)和 Issue #436 中。

我还做了一些其他更改:

  1. 我没有使用“硬编码”“HI2”,而是使用 Unicode 符号表示下标 2,“\U2082”

  2. 将绘图调用更改为“标准”ggplot 而不是 qplot。

  3. 从绘制绘图后调用 directlabels 更改为调用 geom_dl 以在“标准”ggplot 工作流程中添加直接标签。

  4. geom_dl中设置fontfamily。我发现下标 2 是用某些字体系列渲染的,但不是其他字体系列。

  5. warn 选项更改为零(默认值),以便警告不会变成错误。我只是在测试代码时这样做的,但如果需要的话,当然可以将其设置回 2。

block myChunk1a创建绘图。 block myChunk1b 创建基本相同的绘图,但有多个版本,每个版本使用不同的字体系列。在这些版本中,您可以看到下标 2 使用某些字体系列呈现,但其他字体系列则不呈现。我不确定是什么决定了这一点,并且您的系统上的结果可能会有所不同。

\documentclass{article}
\begin{document}

<<setup, include=FALSE>>=
options(warn = 0)
options(device = function(file, width = 7, height = 7, ...) {
  cairo_pdf(tempfile(), width = width, height = height, ...)
})
knitr::opts_chunk$set(echo = FALSE, message=FALSE, warning=FALSE, dev="cairo_pdf")
@

<<myChunk>>=
library(ggplot2)
library(directlabels)
library(gridExtra)
library(dplyr)

data(BodyWeight,package="nlme")
BodyWeight$temp <- as.character(BodyWeight$Rat)

BodyWeight$temp[BodyWeight$temp=="4"] = "HI\U2082"

# Change first value so that HI2 label is easily visible
BodyWeight$weight[BodyWeight$temp=="HI\U2082" & BodyWeight$Time==1] = 350
@

<<myChunk1a, fig.height=5>>=
ggplot(BodyWeight, aes(Time, weight, colour=temp)) + 
  geom_line() +
  geom_dl(method=list("first.qp", fontfamily="Helvetica", cex=1), aes(label=temp)) + 
  theme_bw() +
  ggtitle("Helvetica") +
  guides(colour=FALSE)
@

<<myChunk1b, fig.height=11>>=
# Create several plots, each demonstrating a different font family for the labels
grid.arrange(grobs=lapply(c("Helvetica","Courier","Palatino","Times","Serif"), function(f) {
  ggplot(BodyWeight, aes(Time, weight, colour=temp)) + 
    geom_line() +
    geom_dl(method=list("first.qp", fontfamily=f, cex=1), aes(label=temp)) + 
    labs(x="") + 
    theme_bw() +
    theme(plot.margin=unit(c(0,0,0,0), "lines"),
          text=element_text(size=9)) +
    ggtitle(f) +
    guides(colour=FALSE)
}), ncol=1)
@

<<myChunk2, fig.height=5>>=
data(BodyWeight,package="nlme")
BodyWeight$temp <- as.character(BodyWeight$Rat)

# Change first value so that HI2 label is easily visible
BodyWeight$weight[BodyWeight$temp=="4" & BodyWeight$Time==1] = 350

# Set temp==4 to desired expression
BodyWeight$temp[BodyWeight$temp == "4"] <- paste(expression(HI[2]))

# Convert temp to factor to set order
BodyWeight$temp = factor(BodyWeight$temp, levels=unique(BodyWeight$temp))

qplot(Time, weight, data=BodyWeight, colour=temp, geom="line") +
  guides(colour=FALSE) +
  geom_text(data=BodyWeight %>% group_by(temp) %>%
              filter(Time == min(Time)), 
            aes(label=temp, x=Time-0.5, y=weight), parse=TRUE, hjust=1) +
  theme_bw()
@

\end{document}

以下是 myChunk1a 中的绘图:

enter image description here

关于r - knitr:无法创建带有 utf-8 字符的图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39338168/

相关文章:

r - 我是否排除训练集中使用的数据来运行预测()模型?

r - Dygraph with R - 如何使用axisLabelFormatter 作为标签的字符大小?

sql-server - 切换到 UTF-8 时最大的风险是什么?

php - 编码 hell 。 MySQL、PHP 和 utf-8

mysql - SQL 值得从 utf8_general_ci 更新到 utf8_unicode_ci 吗?

php - 将 CSV 中的中文字符插入 MySQL 时遇到问题

r - 在 Anaconda 中安装 R 需要很长时间

python - 将数字字符引用符号转换为 unicode 字符串

c - 如何在 C 中从八进制 ISO-8859-1 转储 utf8

r - 绘制 unicode 字符 Windows OS R