使用cairo_pdf 设备似乎可以解决此问题。在下面的setup 块中,我将设备选项设置为cairo_pdf 设备(即以option(device = ... 开头的行)和全局块选项dev 默认为“cairo_pdf”(在以@ 开头的行中) 987654330@)。这种方法在knitr documentation(参见多字节字符编码部分)和Issue #436 中进行了讨论。
我还做了一些其他更改:
我使用 Unicode 符号代替“硬编码”"HI₂" 下标 2,"\U2082"。
将绘图调用更改为“标准”ggplot 而不是 qplot。
将绘图后调用 directlabels 更改为调用 geom_dl 以在“标准”ggplot 工作流程中添加直接标签。
在geom_dl 中设置fontfamily。我发现下标 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}
myChunk1a 的情节如下所示: