在一个 Knitr 文档中,我使用了一个自定义标签函数,它输出了一些 LaTeX 代码(这里没用,只是为了说明问题)。在正常图中,一切都很好:R 设法正确猜测输出的指标并留下合理的空间(中间图)。但是当使用构面时,这些指标只是使用纯字符串,而不是像我们期望的非 LaTeX 输出设备(最后一个图)那样将其解释为 LaTeX(第一个图)。
如何说服 ggplot始终将我的标签解释为 LaTeX 并使用智能指标?
\documentclass{beamer}
\setbeamertemplate{navigation symbols}{}
\usepackage{siunitx}
\begin{document}
\begin{frame}
<<dev='tikz', echo=F, fig.height=1.5>>=
library(ggplot2)
mylabel <- function() {
function(xs) { sprintf("\\ensuremath{\\num{%g}}",xs) }
}
data <- data.frame(x=c(1,10), y=c(1,10), kind=c("a","a"))
ggplot(data,aes(x=x,y=y)) + geom_point() +
scale_y_continuous(labels=mylabel()) +
facet_grid(~ kind)
@
<<dev='tikz', echo=F, fig.height=1.5>>=
ggplot(data,aes(x=x,y=y)) + geom_point() +
scale_y_continuous(labels=mylabel())
@
<<echo=F, fig.height=1.5>>=
ggplot(data,aes(x=x,y=y)) + geom_point() +
scale_y_continuous(labels=mylabel())
@
\end{frame}
\end{document}