使用Knitr将绘图居中到页面中间

Dam*_*ian 12 pdf r rstudio knitr

我想将绘图与编织生成的pdf文档的页面中心对齐.我可以使用水平对齐绘图到中心,fig.align='center'但无法弄清楚如何使绘图垂直对齐到中心.

我一直在使用以下代码:

---
header-includes: \usepackage{graphicx}
output: 
  pdf_document
geometry:
  left=1in,right=1in,top=1in,bottom=1in
---


```{r,fig.align='center',out.extra='angle=90', echo=FALSE}

library(ggplot2)
ggplot(diamonds, aes(y=carat, x=price, colour=clarity))+geom_point()+
facet_wrap(~cut)

```
Run Code Online (Sandbox Code Playgroud)

CL.*_*CL. 12

在LaTeX方面,垂直居中的数字必须是figure有位置的p.如何使用knitr取决于:

  • 如果图中有标题,则将其置于figure环境中(请参阅参考资料fig.env).然后只fig.pos = 'p'需要附加选项.
  • 如果图中没有标题(通常是坏标题),您可以手动添加figure环境:

    \begin{figure}[p]
    
    ```{r,fig.align='center',out.extra='angle=90', echo=FALSE}
    
    library(ggplot2)
    ggplot(diamonds, aes(y=carat, x=price, colour=clarity))+geom_point()+
    facet_wrap(~cut)
    
    ```
    \end{figure}
    
    Run Code Online (Sandbox Code Playgroud)

请注意,这在编译为PDF时有效,但会将PDF限制为输出格式.