Rmarkdown/knitr子图不同图形尺寸 [英] Rmarkdown / knitr subfigure different figure sizes

查看:781
本文介绍了Rmarkdown/knitr子图不同图形尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为Rmarkdown中的子图实现不同的高度和宽度.我希望只为fig.heightfig.width提供一个向量,因为这似乎对out.heightout.width

I am trying to achieve different heights and widths for subfigures in Rmarkdown. I was hoping that just providing fig.height and fig.width with a vector each would work, as this does seem to work for out.height and out.width

---
title: "Untitled"
output: pdf_document
header-includes:
   - \usepackage{subfig}
---



```{r, echo = FALSE, fig.height = c(1,2,3), fig.width=c(1,1,1), fig.cap='Caption', fig.subcap=c('Subcaption 1', 'Subcaption 2', 'Subcaption 3')}
library(ggplot2)
df <- data.frame(
  x = rnorm(30),
  y = rnorm(30)
)
p1 <- p2 <- p3 <- ggplot(df, aes(x, y)) +
  geom_point()

print(p1)

print(p2)

print(p3)
```

但是,结果是这样

因此,似乎所有子图都使用值fig.height = 3,fig.width = 1

So, it seems that all subfigures use the values fig.height = 3, fig.width= 1

有人知道如何分别为每个子图指定这些值吗?

推荐答案

这些并不是真正的子图",它们只是并排的数字.因此,您可以通过输入更多内容来获得所需的内容:

Those aren't really "subfigures", they are just side-by-side figures. So you can get what you want with some more typing:

```{r, echo = FALSE}
library(ggplot2)
df <- data.frame(
  x = rnorm(30),
  y = rnorm(30)
)
p1 <- p2 <- p3 <- ggplot(df, aes(x, y)) + geom_point()
```
```{r echo = FALSE, fig.height=1, fig.width=1}
print(p1)
```
```{r echo = FALSE, fig.height=2, fig.width=1}
print(p2)
```
```{r echo = FALSE, fig.height=3, fig.width=1}
print(p3)
```

您可以使用>://://yihui.org/knitr/演示/参考/,但我不确定是否值得这么做.另一种有前途的方法是使用gridExtra::grid.arrange函数,尽管我不确定它是否允许您想要的布局.

You could probably automate this a little using ideas from https://yihui.org/knitr/demo/reference/, but I'm not sure it's worth the trouble. Another promising approach would be to use the gridExtra::grid.arrange function, though I'm not sure if it would allow the layout you want.

在评论后添加编辑,表明需要真正的LaTeX子图:

EDITED TO ADD after the comment indicating that true LaTeX subfigures are wanted:

这比较困难,因为如您所见,对于每个子图,fig.height均未单独处理.我认为您可以通过增加额外的利润来获得想要的高度.要完全控制垂直居中,您需要使用YAML选项关闭图形的裁剪

This is harder, because as you saw, fig.height is not treated separately for each subfigure. I think you can get the heights you want by adding extra margins. To have full control over vertical centering, you need to turn off cropping of the figures using the YAML option

output: 
  pdf_document:
    fig_crop: FALSE

使用该选项时,此代码

```{r, echo = FALSE, fig.height=3, fig.width=1,fig.subcap=c("first", "second", "third"),fig.cap="Main"}
library(ggplot2)
df <- data.frame(
  x = rnorm(30),
  y = rnorm(30)
)
p1 <- p2 <- p3 <- ggplot(df, aes(x, y)) + geom_point()
p1 + theme(plot.margin = margin(t = 1, b = 1, unit = "in") + theme_get()$plot.margin)
p2 + theme(plot.margin = margin(t = 1/2, b = 1/2, unit = "in") + theme_get()$plot.margin)
p3
```

提供以下输出:

这篇关于Rmarkdown/knitr子图不同图形尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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