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

标签 r pdf rstudio 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 作为输出格式。

    关于r - 使用 Knitr 将绘图居中到页面中间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33922757/

    相关文章:

    python - OpenCV 中模板匹配的分辨率操作

    java - 在 RStudio 中使用 iplots 库运行代码失败

    r - 使用 for 循环绘制多条 ROC 曲线

    r - 根据列值计算平均值

    ios - 从 iOS 应用程序打开 PDF 文件

    node.js - Cloud Functions for Firebase - 将 PDF 转换为图像

    r - Rstudio中的表示-使图像填充整个屏幕

    R Plot_ly 列出在 .Rmd R-Studio 运行模式下绘制多个图,但在针织时不绘制

    r - `nlme` 具有交叉随机效应

    javascript - R Shiny 仪表板: capture click event on dashboard body