0

I'm trying to knit a lattice contourplot into a PDF document using knitr and the tikz device but am getting an Error in getMetricsFromLaTeX(TeXMetrics) on compile. Here's the minimal reproducible example:

\documentclass{article}

\begin{document}

<<contourplot,dev='tikz',echo=FALSE>>=
library(lattice)
library(RCurl)
x <- getURL("https://dl.dropboxusercontent.com/u/3966900/likelihoods.csv")
likelihoods <- read.csv(text=x)

cutoffs <- c(-    Inf,-2700,-1497,-1486.6,-1486.3,-1486.286,-1486.28513,-1486.285082,-1486.28508033,-1486.285080237,    Inf)
contourplot(ll ~ var1*var2,
                           data = likelihoods,
                           scales = list(y = list(log = 10)),
                           at=cutoffs,
                           label.style='align',
                           labels=as.character(cutoffs),
                           xlab='$\\\\\\sigma$')
@

\end{document}

The whole thing works if I remove the scales line (I assume it's the ^ in the axis labels that trips tikz up?) but looks like shit.

It also works if I add sanitize=TRUE to the chunk options and remove two backslashes from the xlab string. In this case, however, the axis label also gets sanitized and I don't get a LaTeX-typeset axis label.

How do I get all of this to work?

RoyalTS
  • 9,545
  • 12
  • 60
  • 101
  • Any reason for not using ggplot? That works well with knitr. For example: `< – Andy Clifton Aug 21 '14 at 23:37
  • I tried `ggplot` (a package I have loads more experience with than `lattice`) but never got the plot to look like the one above looks. But after playing around with this for a bit more, perhaps that was premature. This does almost exactly what I want: http://stackoverflow.com/questions/19658088/custom-levels-in-ggplot2-contour-plot – RoyalTS Aug 22 '14 at 00:03
  • You could use `scales = list(y = list(log=10, at=10**seq(5,15,5))))` or `scales = list(y = list(log=10, at=10**seq(5,15,5), label=sprintf("$10^{%d}$", seq(5,15,5))))` in `countourplot()` – rcs Aug 25 '14 at 10:18
  • I'm pretty happy with Andy's `ggplot` solution, but since your solution actually answers the original question, do you want to post it as an answer and collect the bounty? – RoyalTS Aug 25 '14 at 12:18

2 Answers2

1

As Andy Clifton suggests in comments, doing this with ggplot seems to be a hell of a lot easier than getting it to work with lattice:

\documentclass{article}

\begin{document}

<<contourplot,dev='tikz',echo=FALSE,warning=FALSE>>=
library(ggplot2)
library(scales)
library(RCurl)
x <- getURL("https://dl.dropboxusercontent.com/u/3966900/likelihoods.csv")
likelihoods <- read.csv(text=x)

cutoffs <- c(-Inf,-2700,-1497,-1486.6,-1486.3,-1486.286,-1486.28513,-1486.285082,-1486.28508033,-1486.285080237,Inf)

v <- ggplot(likelihoods, aes(var1, var2, z = ll))
v <- v + stat_contour(breaks=cutoffs)
v <- v + scale_y_continuous(trans=log10_trans(),
                            breaks=c(1e-5,1e0,1e5,1e10,1e15,1e20),
                            expand = c(0, 0))
v <- v + scale_x_continuous(limits = c(-3, 5),
                            expand = c(0, 0))
v <- v + theme_bw()
v <- v + ylab('$\\sigma$')
v
@

\end{document}
RoyalTS
  • 9,545
  • 12
  • 60
  • 101
  • Note to self - answer the question rather than making a comment! Glad that worked though. – Andy Clifton Aug 22 '14 at 00:46
  • Sorry, didn't mean to steal your credit. Based on your comment I worked out the details in minutes and thought it'd be easiest to just post the solution. I'd be happy to delete my answer, let you repost it and award you the bounty. – RoyalTS Aug 22 '14 at 00:58
  • No worries - only joking. I have no idea how you got it working, but you did, so it's all yours. – Andy Clifton Aug 22 '14 at 01:09
1

To sanitize the axis labels, modify the scales argument in the following way:

# e-notation (like ggplot2)
scales = list(y = list(log=10, at=10**seq(5, 15, 5))))

or

scales = list(y = list(log=10, at=10**seq(5, 15, 5),
              label=sprintf("$10^{%d}$", seq(5, 15, 5))))
rcs
  • 67,191
  • 22
  • 172
  • 153