0

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}

enter image description here

pascal
  • 2,623
  • 2
  • 20
  • 30

1 Answers1

0

I don't really know if this helps...

but have you seen this post? How to use subscripts in ggplot2 legends [R]

have you tried using:

scale_y_continuous(labels=c(expression(paste(_________)), expression(paste(_________))))

where ______ is the expression you want?

Community
  • 1
  • 1
  • Unfortunately I can't use that because I'd like to use custom TeX code (like `\num` in the question which handles showing `1.2e5` in a proper format for example, would be annoying to have to replicate that in R) – pascal Dec 03 '14 at 20:20
  • I really don't understando much about what you want... bu have you seen this? https://github.com/crowding/analysis/blob/master/occlusion.R – Rodrigo de Alexandre Dec 05 '14 at 04:31