3

Here's my code that's supposed to display to graphics next to each other, but fails to do so. In fact the sweave part is not interpreted.

\begin{figure}[h]
\begin{center} 
\begin{minipage}[t]{.485\linewidth} % 
 <<fig=true,echo=false>>=
 print(graph2)
 @
 \newline{\color{red}{\caption{\label{idx}Graph one}}}    
 \end{minipage}
 \hspace{.02\linewidth}
  \begin{minipage}[t]{.485\linewidth}% 
  <<fig=true,echo=false>>=
 print(graph2)
 @
 \newline{\color{red}{ \caption{\label{pb}Graph two}}}
 \end{minipage}

 \end{center}
 \end{figure}

graph1,graph2 is just any given graph created by qplot. Both graphs work just fine outside a minipage. I know this topic has been around, but somehow I could not get solutions to got that worked for others like this one.

Plus I have a little side question: What's the argument to prevent Sweave from generating both .eps and .pdf ? The manual just states that it is the default. However I am sure that I just use pdflatex and hence do not need .eps.

3
  • Are you missing an @ sign after the second instance of print(graph2) ?
    – nullglob
    Commented Jul 30, 2010 at 13:28
  • 3
    In the .Rnw file \SweaveOpts{eps=FALSE, pdf=TRUE} or in the Sweave call: Sweave(file, driver=RweaveLatex(eps=FALSE))
    – rcs
    Commented Jul 30, 2010 at 14:11
  • thx. code is edited, the second @ wasn't missing. unfortunately not the problem. Thx for answering my side question too :) Commented Jul 30, 2010 at 18:37

2 Answers 2

3

Eh, this is actually cheating, but a found a nice workaround on John's blog. It's not using minipage but it's getting it done by using subfigure. Subfigure did not have any problems with Sweave. Nice!

If you are interested in this solution check this site. Still i´d like to know how to do it with minipage :)

2

Replacing \hspace with \hfill does the trick. The plots are from the ggplot documentation. minipage also works nicely for putting two xtable side by side, or a table and a plot.

\documentclass{article}
\usepackage{color}
\begin{document}

\begin{figure}[h]
\begin{center} 

\begin{minipage}[t]{.49\linewidth} % 
<<fig=true,echo=false>>=
require(ggplot2)

df <- data.frame(gp = factor(rep(letters[1:3], each = 10)),
                 y = rnorm(30))
library(plyr)
ds <- ddply(df, .(gp), summarise, mean = mean(y), sd = sd(y))

ggplot(df, aes(x = gp, y = y)) +
   geom_point() +
   geom_point(data = ds, aes(y = mean),colour = 'red', size = 3)

@
\newline{\color{red}{\caption{\label{idx}Graph one}}}    
\end{minipage}
\hfill
\begin{minipage}[t]{.49\linewidth}
<<fig=true,echo=false>>=
ggplot() +
  geom_point(data = df, aes(x = gp, y = y)) +
  geom_point(data = ds, aes(x = gp, y = mean),
                        colour = 'red', size = 3) +
  geom_errorbar(data = ds, aes(x = gp, y = mean,
                    ymin = mean - sd, ymax = mean + sd),
                    colour = 'red', width = 0.4)
@
\newline{\color{red}{ \caption{\label{pb}Graph two}}}
\end{minipage}

\end{center}
\end{figure}

\end{document}

enter image description here

1
  • I get a message when I create the PDF with Sweave from R Studio: "Issues: 1 badbox". However, the pdf output is generated and the figures look fine! Thanks! Commented Mar 31, 2016 at 20:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.