使用Knitr将绘图居中到页面中间 [英] Centre a plot to the middle of a page using Knitr

查看:101
本文介绍了使用Knitr将绘图居中到页面中间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'd like to align a plot to the center of a page of a knitr-generated pdf document. I can horizontally align the plot to the center using fig.align='center' but can't figure out how to get the plot vertically aligned to the center.

我一直在使用以下代码:

I've been using the following code:

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

```

推荐答案

在LaTeX一侧,垂直居中的图形必须是 figure,位置为p .使用knitr如何实现此目标取决于:

On the LaTeX side, a vertically centered figure must be a figure with position p. How this can be achieved using knitr depends:

  • If the figure has a caption, it is placed in a figure environment (see fig.env). Then only the additional option fig.pos = 'p' is needed.
  • If the figure has no caption (which is usually bad), you can manually add the figure environment:

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

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

Note that this works when compiling to PDF but restricts you to PDF as output format.

这篇关于使用Knitr将绘图居中到页面中间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆