r - 使用Knitr和RStudio将ggplot2输出嵌入LaTeX pdf中

标签 r latex ggplot2 knitr rstudio

过去,我曾使用RStudio创建ggplot2,然后从RSudio中将它们导出为PDF。这很棒。

我现在正在尝试使用knitr进行自动化,但是在弄清楚在何处设置图形高度和权重以创建高质量输出时遇到了麻烦。

这是我目前的尝试,但是“并排”图不是,旋转横向图也不是,分辨率似乎很低。

我将不胜感激任何建议。似乎ggplot2和knitr都在积极开发中,这很好,但是,互联网搜索已使我很努力。 LaTeX对我来说是新的。我还要感谢这套工具的所有常规工作流程策略。提前致谢。

\documentclass[letterpaper]{article}
\usepackage{lscape}
\begin{document}
<<load, echo=FALSE, results='hide', warning=FALSE, message=FALSE>>=
require(ggplot2)
@ 

Two on the first page.
<<first, echo=FALSE, fig.height=3, fig.cap="This is first", fig.pos='h'>>=
ggplot(mtcars, aes(mpg, wt))+geom_point()+facet_grid(vs ~ am, margins=TRUE)
@

Blah, blah, blah.
<<second, echo=FALSE, fig.height=3, fig.cap="This is second", fig.pos='h'>>=
ggplot(mtcars, aes(mpg, wt))+geom_point()+facet_grid(vs ~ am, margins=TRUE)
@
\newpage
Second page.

Side by side images:

<<third, echo = FALSE, out.width="2in", fig.cap='Side by side'>>=
ggplot(mtcars, aes(mpg, wt))+geom_point()+facet_grid(vs ~ am, margins=TRUE)
ggplot(mtcars, aes(mpg, wt))+geom_point()+facet_grid(vs ~ am, margins=TRUE)
@
\newpage
\begin{landscape}
This page is rotated
<<fourth, echo = FALSE, out.width="4in", fig.cap='Landscape'>>=
ggplot(mtcars, aes(mpg, wt))+geom_point()+facet_grid(vs ~ am, margins=TRUE)
@
\end{landscape}
\end{document}

最佳答案

我可以为您提供大部分帮助:

\documentclass[letterpaper]{article}
\usepackage{lscape}
\usepackage{float}
\begin{document}
<<load, echo=FALSE, results='hide', warning=FALSE, message=FALSE>>=
require(ggplot2)
@ 

Two on the first page.
<<first, echo=FALSE, fig.height=3, fig.cap="This is first", fig.pos='h'>>=
ggplot(mtcars, aes(mpg, wt))+geom_point()+facet_grid(vs ~ am, margins=TRUE)
@

Blah, blah, blah.
<<second, echo=FALSE, fig.height=3, fig.cap="This is second", fig.pos='h'>>=
ggplot(mtcars, aes(mpg, wt))+geom_point()+facet_grid(vs ~ am, margins=TRUE)
@

\newpage
Second page.

Side by side images:

\begin{figure}[H]
<<third, echo = FALSE, out.width="0.48\\linewidth",fig.width = 3.5,fig.height=2>>=
ggplot(mtcars, aes(mpg, wt))+geom_point()+facet_grid(vs ~ am, margins=TRUE)
ggplot(mtcars, aes(mpg, wt))+geom_point()+facet_grid(vs ~ am, margins=TRUE)
@
\caption{Side by side}
\end{figure}

\newpage
\begin{landscape}
This page is rotated.
<<fourth, echo = FALSE, fig.width = 4,fig.height = 3,out.width = "0.9\\linewidth">>=
ggplot(mtcars, aes(mpg, wt))+geom_point()+facet_grid(vs ~ am, margins=TRUE)
@
\end{landscape}
\end{document}


质量对我来说看起来不错,但前提是我使用系统PDF查看器(Preview,OS X)。内置的RStudio PDF查看器过去对我来说存在一些渲染问题,因此我不使用它。

我不确定如何强制横向页面上的图形位于文本下方。通常,我会像前面的图一样使用float包来执行此操作,但是它似乎在横向环境中不起作用。我建议您在tex.stackexchange.com上咨询相关人员,因为它是LaTeX特有的。

不是fig.widthfig.heightout.width之间的相互作用。两者一起玩,看看图像大小与图像中元素的缩放比例发生了什么变化。一个影响创建图形时的实际图形尺寸,另一个影响当将图像包含在LaTeX文档中时如何缩放图像(我认为)。

另请注意,我在图形环境中并排使用了\caption{},因为否则它将尝试为每个图形创建标题。

关于r - 使用Knitr和RStudio将ggplot2输出嵌入LaTeX pdf中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14074732/

相关文章:

r - R错误消息中的函数sweep()

在 Beamer 中包含/排除定义的帧

r - 在 ggplot2 图中自动使用标签(避风港语义)

r - 使用 stat_count 时将百分比标签添加到 ggplot

r - 在 ggsurvplot 中包含 weibull 拟合

r - 在R中将字符串拆分为不同长度的子字符串

r - 以交互方式保存文件?

r - ggplot2 堆积条形图 - 每个条形图均为 100%,每个条形图内都有百分比标签

latex - 我可以将LaTeX宏 'return'设为文件名吗?

latex - 在 Rstudio 中合并 rmarkdown 生成的 latex 文档,而无需手动删除序言