Xu *_*ang 4 encoding r 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}
Run Code Online (Sandbox Code Playgroud)
以下是我从 R 调用 knitr 的方式:
library(knitr)
# I have tryied this but doesn't make difference:
# pdf.options(encoding='ISOLatin2.enc')
knit("mwe_knitr.Rnw")
Run Code Online (Sandbox Code Playgroud)
我得到以下输出:
> 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>
Run Code Online (Sandbox Code Playgroud)
我尝试了编码的解决方案,例如发布在这里: Rhtml:警告:'mbcsToSbcs'中'<var>'的转换失败:点替换<var>
(我在上面的评论中指出我尝试解决该问题的确切方法)但它似乎对我没有任何改变。
我在 Ubuntu 上使用 R 3.3.1 和 knitr 包 1.13。
使用该cairo_pdf设备似乎可以解决此问题。在setup下面的块中,我将设备选项设置为cairo_pdf设备(即开始的行option(device = ...),并将全局块选项设置dev为默认为“cairo_pdf”(在开始的行中knitr::opts_chunk$set(...)。knitr文档(参见多字节字符编码部分)和问题 #436 中讨论了这种方法。
我做了一些其他的改变:
"HI?"我没有使用“硬编码”,而是将 Unicode 符号用于下标 2, "\U2082".
将绘图调用更改为“标准”ggplot 而不是 qplot。
从directlabels制作绘图后调用更改为调用geom_dl以在“标准”ggplot 工作流程中添加直接标签。
设置fontfamily内geom_dl。我发现下标 2 是用一些字体系列渲染的,而不是其他字体系列。
将warn选项更改为零(默认值),这样警告就不会变成错误。我只是在测试代码时这样做了,但当然,如果需要,可以将其设置回 2。
块myChunk1a创建情节。该块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}
Run Code Online (Sandbox Code Playgroud)
这是情节的myChunk1a样子: