使用 Knitr 将绘图居中放置在页面中间
Centre a plot to the middle of a page using Knitr
我想将绘图与 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)
```
在 LaTeX 方面,垂直居中的图形必须是 figure
with position 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}
请注意,这在编译为 PDF 时有效,但将您限制为 PDF 作为输出格式。
我想将绘图与 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)
```
在 LaTeX 方面,垂直居中的图形必须是 figure
with position 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}
请注意,这在编译为 PDF 时有效,但将您限制为 PDF 作为输出格式。