使用Knitr将绘图居中到页面中间 [英] Centre a plot to the middle of a page using 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:
- 如果图形有标题,则将其放置在
figure
环境中(请参阅fig.env
).然后,仅需要附加选项fig.pos = 'p'
. -
如果图形没有标题(通常很糟糕),则可以手动添加
figure
环境:
- If the figure has a caption, it is placed in a
figure
environment (seefig.env
). Then only the additional optionfig.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屋!