【问题标题】:Issues plotting multiple plots in a single figure within RMarkdown在 RMarkdown 中的单个图中绘制多个图的问题
【发布时间】:2018-08-29 23:04:35
【问题描述】:

我有绘图功能,可以产生四个像 glm 一样的绘图。一个直方图、一个箱线图和两个散点图。但我无法将它们放在文档中的正确位置,以便查看所有信息。我想要 1 行中的 2 个图并且很宽,以便我可以看到标题等所有信息。

贴出我的绘图功能是没有意义的,因为它是为我创建的一个类。

我的输出看起来总是这样

我有以下 YAML 布局:

---
title: 
author:
fontsize: 11
graphics: true
documentclass: article
output: 
  pdf_document:
    fig_caption: yes
    toc: true
    latex_engine: pdflatex
    toc_depth: 2
    number_sections: true
    keep_tex: true
header-includes: 
- \usepackage{graphicx}
- \usepackage{float}
- \usepackage{subfig}
---

```{r setup, include = FALSE}
library(knitr)
knitr::opts_chunk$set(fig.path = 'figures/', fig.pos = 'htb!', echo = TRUE)
knit_hooks$set(plot = function(x, options)  {
  hook_plot_tex(x, options)
})
```

```{r, out.width='1\\linewidth', fig.asp=0.5, fig.ncol = 1, fig.cap="output",fig.align = "center"}
par(mfrow = c(1, 2))
plot(poisReg)
```

【问题讨论】:

  • 始终确保您的问题是可重复的。如果您不想包含绘图功能,则应尝试包含复制行为的内容。可以用两条plot(cars) 代替吗?
  • 没有两个汽车地块,我得到了我想要的结果,但不是我的地块嗯。也许我应该发布我的整个代码,但它太长了..
  • 您也没有遵循之前推荐的解决方案。没有fig.subcap。另外,你为什么要把它和par(mfrow = c(1, 2))混在一起?
  • 另外,请尝试提供一个最小的工作示例。对于所有的 YAML,knit_hooks,实际上有很多事情可能是错误的。它使回答变得更加困难。我知道你是 StackOverflow 的新手,所以我会努力帮助你变得更好,以便人们更容易为你提供答案。
  • 我终于修好了。问题是我的图形文件夹中有很多图形。我不知道为什么,但您的解决方案没有奏效,我使用了其他工作。感谢您的时间和帮助。你是个好人

标签: r plot r-markdown figure


【解决方案1】:

我无法复制您的问题。使用一组可重现的图:

---
title: 
author:
fontsize: 11
graphics: true
documentclass: article
output: 
  pdf_document:
    fig_caption: yes
    toc: true
    latex_engine: pdflatex
    toc_depth: 2
    number_sections: true
    keep_tex: true
header-includes: 
- \usepackage{graphicx}
- \usepackage{float}
- \usepackage{subfig}
---

```{r setup, include = FALSE}
library(knitr)
knitr::opts_chunk$set(fig.path = 'figures/', fig.pos = 'htb!', echo = TRUE)
knit_hooks$set(plot = function(x, options)  {
  hook_plot_tex(x, options)
})
```

```{r, out.width='1\\linewidth', fig.asp=0.5, fig.ncol = 1, fig.cap="output",fig.align = "center"}
par(mfrow = c(1, 2))
plot(cars, main = "Title 1")
plot(cars, main = "Title 2")
plot(cars, main = "Title 3")
plot(cars, main = "Title 4")
```

在我看来,设置 figures 文件夹的 knitr 搞砸了。

【讨论】:

    猜你喜欢
    • 2021-04-21
    • 1970-01-01
    • 1970-01-01
    • 2014-03-06
    • 2011-10-19
    • 2023-03-27
    • 1970-01-01
    • 2021-01-03
    • 2023-03-28
    相关资源
    最近更新 更多