使用knitr和RStudio在LaTeX pdf中嵌入ggplot2输出

使用knitr和RStudio在LaTeX pdf中嵌入ggplot2输出,r,latex,ggplot2,knitr,rstudio,R,Latex,Ggplot2,Knitr,Rstudio,在过去,我使用RStudio创建ggplot2,然后从RSudio中将它们导出为PDF。这真是妙不可言 我现在正尝试使用knitr实现自动化,但我很难确定在何处设置图形高度和权重以创建高质量的输出 这是我目前的尝试,但“并排”图不是,旋转的风景图不是,分辨率似乎很低 如果有任何建议,我将不胜感激。看起来ggplot2和knitr都在积极开发中,这是很好的,但是,互联网搜索已经引领了我的研究。乳胶对我来说是新的。我也很欣赏这套工具的通用工作流策略。提前谢谢 \documentclass[lette

在过去,我使用RStudio创建ggplot2,然后从RSudio中将它们导出为PDF。这真是妙不可言

我现在正尝试使用knitr实现自动化,但我很难确定在何处设置图形高度和权重以创建高质量的输出

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

如果有任何建议,我将不胜感激。看起来ggplot2和knitr都在积极开发中,这是很好的,但是,互联网搜索已经引领了我的研究。乳胶对我来说是新的。我也很欣赏这套工具的通用工作流策略。提前谢谢

\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[信纸]{article}
\usepackage{lscape}
\开始{document}
=
需要(ggplot2)
@ 
第一页有两个。
=
ggplot(mtcars,aes(mpg,wt))+geom_点()+facet_网格(vs~am,边距=真)
@
废话,废话,废话。
=
ggplot(mtcars,aes(mpg,wt))+geom_点()+facet_网格(vs~am,边距=真)
@
\新页
第二页。
并排图像:
=
ggplot(mtcars,aes(mpg,wt))+geom_点()+facet_网格(vs~am,边距=真)
ggplot(mtcars,aes(mpg,wt))+geom_点()+facet_网格(vs~am,边距=真)
@
\新页
\开始{横向}
此页已旋转
=
ggplot(mtcars,aes(mpg,wt))+geom_点()+facet_网格(vs~am,边距=真)
@
\结束{横向}
\结束{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}
\documentclass[信纸]{article}
\usepackage{lscape}
\usepackage{float}
\开始{document}
=
需要(ggplot2)
@ 
第一页有两个。
=
ggplot(mtcars,aes(mpg,wt))+geom_点()+facet_网格(vs~am,边距=真)
@
废话,废话,废话。
=
ggplot(mtcars,aes(mpg,wt))+geom_点()+facet_网格(vs~am,边距=真)
@
\新页
第二页。
并排图像:
\开始{图}[H]
=
ggplot(mtcars,aes(mpg,wt))+geom_点()+facet_网格(vs~am,边距=真)
ggplot(mtcars,aes(mpg,wt))+geom_点()+facet_网格(vs~am,边距=真)
@
\标题{并排}
\结束{图}
\新页
\开始{横向}
此页已旋转。
=
ggplot(mtcars,aes(mpg,wt))+geom_点()+facet_网格(vs~am,边距=真)
@
\结束{横向}
\结束{document}
质量看起来不错,但只有当我使用我的系统PDF查看器(预览,OSX)时。内置的RStudio PDF查看器在过去对我来说有一些渲染问题,所以我不使用它

我不知道如何强制横向页面上的图形位于文本下方。通常,我会像前面的图一样使用float包,但它在横向环境中似乎不起作用。我建议你咨询一下tex.stackexchange.com上的人,因为这是相当具体的

不是
fig.width
fig.height
out.width
之间的相互作用。使用这两种方法,查看图像大小与图像中元素的缩放比例之间的关系。一个影响创建时的实际图形大小,另一个影响在LaTeX文档中包含该图像时的缩放方式(我认为)


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

不确定旋转的第四页,但要获得并排图,需要
fig.show='hold'
out.width='.45\\linewidth'

<<third, echo = FALSE, out.width="2in", fig.cap='Side by side',out.width='.45\\linewidth',fig.show='hold'>>=
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)
@
=
ggplot(mtcars,aes(mpg,wt))+geom_点()+facet_网格(vs~am,边距=真)
ggplot(mtcars,aes(mpg,wt))+geom_点()+facet_网格(vs~am,边距=真)
@