How to use ggplot2 graphics inside minipage with Sweave?
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}开发者_如何学Python
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.
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 :)
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}
精彩评论