如何通过 Sweave 在 minipage 中使用 ggplot2 图形?

发布于 2024-09-11 23:37:09 字数 810 浏览 4 评论 0原文

这是我的代码,应该显示到彼此相邻的图形,但未能这样做。事实上,sweave 部分没有被解释。

\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 只是 qplot 创建的任何给定图表。这两个图表在小型页面之外都可以正常工作。我知道这个主题已经存在,但不知何故我无法找到解决方案来使其适用于像这样的其他人 一个

另外,我还有一个小问题:阻止 Sweave 生成​​ .eps 和 .pdf 的论点是什么?手册只是说这是默认的。不过我确信我只使用 pdflatex,因此不需要 .eps。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寻找我们的幸福 2024-09-18 23:37:09

呃,这实际上是作弊,但我在约翰的博客上找到了一个很好的解决方法。它没有使用 minipage,而是通过使用 subfigure 来完成它。 Subfigure 使用 Sweave 没有任何问题。好的!

如果您对此解决方案感兴趣,请检查此网站。我仍然想知道如何使用 minipage 来做到这一点:)

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 :)

牵强ㄟ 2024-09-18 23:37:09

\hspace 替换为 \hfill 就可以了。这些图来自 ggplot 文档。 minipage 也可以很好地并排放置两个 xtable,或者一个表格和一个绘图。

\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}

在此处输入图像描述

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文