r - 带网格图形的两列投影仪/摆动幻灯片

标签 r latex ggplot2 beamer sweave

我正在尝试使用 beamer + sweave 对 ggplot2 图形进行演示。有些幻灯片应该有两列;左边的是代码,右边的是生成的图形。这是我尝试过的,

\documentclass[xcolor=dvipsnames]{beamer}
\usepackage{/Library/Frameworks/R.framework/Resources/share/texmf/tex/latex/Sweave}
\usepackage[english]{babel}
\usepackage{tikz}
\usepackage{amsmath,amssymb}% AMS standards
\usepackage{listings}
\usetheme{Madrid}
\usecolortheme{dove}
\usecolortheme{rose}
\SweaveOpts{pdf=TRUE, echo=FALSE, fig=FALSE, eps=FALSE, tidy=T, width=4, height=4}

\title{Reproducible data analysis with \texttt{ggplot2} \& \texttt{R}}
\subtitle{subtitle}
\author{Baptiste Augui\'e}
\date{\today}
\institute{Here}

\begin{document}

\begin{frame}[fragile]
  \frametitle{Some text to show the space taken by the title}
\begin{columns}[t] \column{0.5\textwidth}
  Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. 
\column{0.5\textwidth}  
\begin{figure}[!ht]
\centering
<<fig=TRUE>>=
grid.rect(gp=gpar(fill="slateblue"))
@
\end{figure}
\end{columns}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Some text to show the space taken by the title}
\begin{columns}[t] 
  \column{0.5\textwidth}
<<echo=TRUE,fig=FALSE>>=
library(ggplot2)
p <- 
qplot(mpg, wt, data=mtcars, colour=cyl) + 
  theme_grey(base_family="Helvetica")
@
\column{0.5\textwidth}  
\begin{figure}[!ht]
\centering
<<fig=TRUE>>=
print(p)
@
\end{figure}
\end{columns}
\end{frame}

\end{document}

和输出的两页。

first page

second page

我对这个输出有两个问题:
  • echo-ed sweave 代码忽略列环境并跨越两列
  • 任一图形的列边距过宽

  • 有任何想法吗?

    谢谢。

    最佳答案

  • 对于第一个问题,最简单的方法是设置keep.source=TRUE在 SweaveOpts 中。更多花式控制,见fancyvrb和 Sweave 手册的 FAQ #9。
  • 图的宽度可以通过\setkeys{Gin}{width=1.0\textwidth}设置

  • 这是一个轻微的修改:
    ... snip ...
    
    \SweaveOpts{pdf=TRUE, echo=FALSE, fig=FALSE, eps=FALSE, tidy=T, width=4, height=4, keep.source=TRUE}
    
    \title{Reproducible data analysis with \texttt{ggplot2} \& \texttt{R}}
    
    ... snip ...
    
    
    \begin{document}
    \setkeys{Gin}{width=1.1\textwidth}
    
    ... snip...
    
    <<echo=TRUE,fig=FALSE>>=
    library(ggplot2)
    p <- 
      qplot(mpg,
            wt,
            data=mtcars,
            colour=cyl) +  
      theme_grey(base_family=
                 "Helvetica")
    @
    

    enter image description here

    关于r - 带网格图形的两列投影仪/摆动幻灯片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6964750/

    相关文章:

    r - 无法使用 r 中的 {gtsummary} 包编织为 pdf

    r - 逻辑否定运算符 "!"如何工作

    r - 如何更改跟踪中各段的颜色?

    documentation - 控制 Doxygen 的 LaTeX 输出以制作 PDF 文档

    latex - Natbib 不显示完整的作者姓名

    R ggplot箱线图改变颜色和填充

    r - 使用 scale_y_time 在 Boxplot (GGPLOT) 中将 MS 转换为分和秒

    R ggplot反转轴并控制条形方向

    mysql - 使用 RMySQL 获取的整数列无法转换为 double

    r - dplyr 如何按组滞后