以下是我的.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: myChunkQuitting 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块中,将device选项设置为cairo_pdf设备(以option(device = ...开头的行),并将全局块选项dev缺省设置为“ cairo_pdf”(在以)。在knitr::opts_chunk$set(... documentation(请参见多字节字符编码)和Issue #436中讨论了这种方法。我进行了其他一些更改:我使用Unicode符号代替下标2,knitr,而不是“硬编码” "HI₂"。将绘图调用更改为“标准” ggplot,而不是qplot。从绘制后调用"\U2082"更改为调用directlabels以在“标准” ggplot工作流程中添加直接标签。在geom_dl中设置fontfamily。我发现下标2是用某些字体系列渲染的,但不是用其他字体系列渲染的。将geom_dl选项更改为零(默认值),以使警告不会变成错误。我只是在测试代码时才这样做的,但是如果需要,当然可以将其设置回2。块warn创建图。块myChunk1a创建基本上相同的图,但是在多个版本中,每个版本使用不同的字体系列。在这些版本中,您可以看到下标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 visibleBodyWeight$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 labelsgrid.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 visibleBodyWeight$weight[BodyWeight$temp=="4" & BodyWeight$Time==1] = 350# Set temp==4 to desired expressionBodyWeight$temp[BodyWeight$temp == "4"] <- paste(expression(HI[2]))# Convert temp to factor to set orderBodyWeight$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}这是myChunk1b中的图所示: 08-16 23:36