I am forwarding to r-help just in case anyone else where wondering or have
the same problem running the example.

Cheers
D

---------- Forwarded message ----------
From: Daniel Haugstvedt <daniel.haugstv...@gmail.com>
Date: Sun, Dec 22, 2013 at 11:54 PM
Subject: Re: [R] Knitr, ggplot and consistent fonts
To: John Kane <jrkrid...@inbox.com>


Hi John,

Thank you for taking the time to look at this.

The error is because of the "_" in geom_histogram. "_" is reserved in latex
for subscripts and requires the statement to be in math mode which is  why
latex wants $ before and after $ ($ is used for inline math mode in latex).

 The question now becomes, why is the word geom_histogram in the .tex file.
It should have been compiled to a plot. There are two reasons I can think
of:
1) Could it be that the R code fails and that the error is printed to the
.tex document?
2) Does knitr sends the R code without editing it. Do you have some sort of
default global parameter which is interfering.  e.g. results='asis'?

If you want to get this to work you could try the option results='hide' in
the  <<plot-figHeight>>= chunk. That is the only solution I can think of.


I have been given me some pointers and a link (
https://github.com/wch/ggplot2/wiki/New-theme-system)  which should help me
get this sorted out. Replacing theme_classic()  with  theme_classic(10) got
me almost all the way.

Cheers from Norway
Daniel Haugstvedt


By the way John, I gave LyX a quick try. It seems I have been missing out
on some progress. For me it is not a big problem, but my students could
really benefit from a simpler alternative to latex.






On Sun, Dec 22, 2013 at 7:19 PM, John Kane <jrkrid...@inbox.com> wrote:

> Hi Daniel,
>
> For some reason I cannot get your example to work. The problem is in the
> code chunk but I have no idea what is happening. The code is running
> perfectly in R, itself but LaTeX seems to be choking when it hits the first
> ggplot statement, that is the one in <<plot-figHeight>>=
>
> The message I am getting is: "Missing $ inserted <inserted text> $
> ggplot(df, aes(x=x)) = geom_" and my knowledge of LateX is not enough to
> figure out the problem.
>
> I tried stripping out most of the LaTeX specific verbiage in the code
> chunk and running the code in LyX which I use rather than plain vanilla
> LaTeX and I still cannot get it to work. It is almost as if there is some
> hidden character in the in that piece of code since I can duplicate the
> code myself and I even pasted in most of the geom_histogram code into my
> code chunk and it runs.
>
> John Kane
> Kingston ON Canada
>
>
> > -----Original Message-----
> > From: daniel.haugstv...@gmail.com
> > Sent: Sun, 22 Dec 2013 12:42:50 +0100
> > To: r-help@r-project.org
> > Subject: [R] Knitr, ggplot and consistent fonts
> >
> > Dear R-help
> >
> > I am using Knitr and ggplot to draft an article and have now started to
> > improve on the layout and graphics. So far I have not been able to
> > maintain
> > the same font size for labels in all my figures.
> >
> > My goal is to be able to change the width of the figures while
> > maintaining
> > the same font. This works for the height parameter (example not
> > included).
> >
> > In the true document I also use tikz, but the problem can be reproduced
> > without it.
> >
> > I know the question is very specific, but my understanding is that this
> > combination of packages  is common. (They are really great. Keep up the
> > good work.)  There has to be others facing the same problem and someone
> > must have found a nice solution.
> >
> > Additional attempts from my side which failed are not included in the
> > example. I have tested the Google results i could find without any luck.
> >
> > Cheers
> > Daniel
> >
> > PS. I know the example plots could have been smaller, but they just
> > became
> > too ugly for me
> >
> >
> > \documentclass{article}
> > \begin{document}
> >
> > <<setup, include=FALSE, cache=FALSE>>=
> > library(knitr)
> > library(ggplot2)
> > @
> >
> > \title{Knitr and ggplot2}
> > \author{Daniel Haugstvedt}
> >
> > \maketitle
> >
> > There are four plots in this article. Figure \ref{fig:plot-figHeight}
> > uses
> > the argument fig.height=2.5 while Figures \ref{fig:plot-figWidth}
> > used both fig.height=2.5 and fig.width=3. The later option makes the font
> > too big.
> >
> > An alternative approach is used in Figures  \ref{fig:plot-figOutWidthBig}
> > and
> >  \ref{fig:plot-figOutWidthSmall}. There the argument out.width is set to
> >  12 and 8 cm respectively. This stops the problem of excessively large
> > fonts
> >  for figures with smaller width, but there is still no consistency
> >  across plots in terms of font size.
> >
> > <<plot-figHeight, echo=FALSE, fig.height=2.5, fig.cap="Density plot with
> > no
> > fig.width argument", fig.pos='ht'>>=
> > df = data.frame(x = rnorm(100), y = 1:100)
> > ggplot(df, aes(x = x)) +
> >   geom_histogram(aes(y = ..density..),
> >                  binwidth = 1, colour = "black", fill = "white") +
> >   xlab("Improvement, %") +
> >   ylab("Density") +
> >   theme_classic()
> > @
> >
> > <<plot-figWidth, echo=FALSE, fig.height=2.5, fig.width = 3,
> > fig.cap="Density plot with fig.width=3", fig.pos='ht'>>=
> > ggplot(df, aes(x = x)) +
> >   geom_histogram(aes(y = ..density..),
> >                  binwidth = 1, colour = "black", fill = "white") +
> >   xlab("Improvement, %") +
> >   ylab("Density") +
> >   theme_classic()
> > @
> >
> > <<plot-figOutWidthBig, echo=FALSE, fig.height=2.5, out.width = "12cm",
> > fig.cap="Density plot with out.width=12cm", fig.pos='ht'>>=
> > ggplot(df, aes(x = x)) +
> >   geom_histogram(aes(y = ..density..),
> >                  binwidth = 1, colour = "black", fill = "white") +
> >   xlab("Improvement, %") +
> >   ylab("Density") +
> >   theme_classic()
> > @
> >
> > <<plot-figOutWidthSmall, echo=FALSE, fig.height=2.5, out.width = "8cm",
> > fig.cap="Density plot with out.width=8cm", fig.pos='ht'>>=
> > ggplot(df, aes(x = x)) +
> >   geom_histogram(aes(y = ..density..),
> >                  binwidth = 1, colour = "black", fill = "white") +
> >   xlab("Improvement, %") +
> >   ylab("Density") +
> >   theme_classic()
> > @
> >
> > \end{document}
> >
> >       [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help@r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> > http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
>
> ____________________________________________________________
> GET FREE SMILEYS FOR YOUR IM & EMAIL - Learn more at
> http://www.inbox.com/smileys
> Works with AIM®, MSN® Messenger, Yahoo!® Messenger, ICQ®, Google Talk™ and
> most webmails
>
>
>

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to