In an Knitr document, I'm using a custom label function which outputs some LaTeX code (it's useless here, just to illustrate the problem). In normal plots, everything is fine: R manages to correctly guess the metrics of the output and leaves a reasonable space (middle plot). But when using facets, these metrics are simply using the plain string, not interpreting it as LaTeX (first plot), as we would expect from a non-LaTeX output device (last plot).
How can I convince ggplot to always interpret my label as LaTeX and use the smart metrics?
\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}